Wednesday, June 14, 2017

Check for sheets in excel before running macro

I am not VBA Expert.  The blog is mostly for my self to remember how to do things that I have learned.

One of the things I have been trying to do is make sure that macros I make don't break when I am gone as I am taking another position.  This little bit of code makes sure that if you are importing a new sheet and then pulling data from it (which I do sometimes) the macro will not run unless the sheet is there.

    Dim sh As Worksheet, flg As Boolean
    For Each sh In Worksheets
    If sh.Name Like "RecR*" Then flg = True: Exit For
    If flg = False Then
    MsgBox "New Sheet Not Found"
    Exit Sub
   
    End If
    Next

No comments:

Post a Comment