+ Reply to Thread
Results 1 to 4 of 4

Validating vba statements if false continue

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2010
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    5

    Validating vba statements if false continue

    Hi,

    Is there a way to validate vba statements? For example, I have some code that opens specific workbooks in a directory. Currently, if a workbook with a certain name is not found the macro will stop running and throw an error message. I would like to know if there is code that will check if that statement is false (it can't find the element it's looking for) and just continue to the next line of code instead of throwing an error message.

    Thanks.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Validating vba statements if false continue

    One way:
        Dim wkb As Workbook
        
        ' ... other code ...
        
        Set wkb = Nothing
        On Error Resume Next
        Set wkb = Workbooks("Bob.xls")
        On Error GoTo 0
        If wkb Is Nothing Then Exit Sub
    
        ' carry on ...
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-05-2010
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Validating vba statements if false continue

    Thanks for the response.

    Will this work for any code statement? Or is there a more generic way to check if a statement returns error?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Validating vba statements if false continue

    More generically, you can do something like this:
        On Error Resume Next
        Set wkb = Workbooks("Bob.xls")
        If Err.Number Then
            ' there was an error
        End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1