Share via

vba error checking if sheet not found

Anonymous
2015-05-20T22:32:56+00:00

I've made button on the quick access toolbar that runs a macro to remove chopped up conditional formatting and put clean formatting back in.

The problem is I want this to work in 2 workbooks that do not have the same sheet names. The "Report" sheet is in one workbook and "Data Input" and "XML" sheets are in the other workbook. I need some error checking, so that if the macro does not find a sheet, move on to the next sheet. If sheet not found, move on to the next sheet.

First I remove existing conditional formatting, then go to the first sheet "Report". If it does not find "Report", jump down and try to find "Data Input" sheet.

On Error GoTo Err1

Sheets("Report").Select

CF code

.

Err1

Resume

On Error GoTo Err2

Sheets("Data Input").Select

CF code

.

Err2

Resume

On Error GoTo Err3

Sheets("XML").Select

CF code

.

Err3

Resume

End Sub

Thanks once again for your help.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-06-14T15:31:07+00:00

Hi Wood13,

Replace the second line of the function 

Function WorksheetExists(sName As String) As Boolean

    SheetExists = Application.Evaluate("ISREF('" & sName & "'!A1)")

End Function

with:

      WorksheetExists = Application.Evaluate("ISREF('" & sName & "'!A1)")

===

Regards,

Norman

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Rory Archibald 18,965 Reputation points Volunteer Moderator
2015-05-21T07:58:11+00:00

You could also use a simple function instead of error handling:

If WorksheetExists("Report") Then

    Sheets("Report").Select

    CF code

End If

If WorksheetExists("Data Input") Then

    Sheets("Data Input").Select

    CF code

End If

If WorksheetExists("XML") Then

    Sheets("XML").Select

    CF code

End If

End Sub

Function WorksheetExists(sName As String) As Boolean

    SheetExists = Application.Evaluate("ISREF('" & sName & "'!A1)")

End Function

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2015-05-21T02:26:25+00:00

Wood13 wrote:

On Error GoTo Err1

Sheets("Report").Select

CF code

.

Err1

Resume

On Error GoTo Err2

Sheets("Data Input").Select

CF code

.

Err2

Resume

On Error GoTo Err3

Sheets("XML").Select

CF code

.

Err3

Resume

End Sub

If "CF code" is the same in all cases and only the worksheet name differs, you might consider:

On Error Resume Next

Sheets("Report").Select

If Err <> 0 Then Err.Clear: Sheets("Data Input").Select

If Err <> 0 Then Err.Clear: Sheets("XML").Select

If Err = 0 Then

    CF code

End If

On Error GoTo 0    ' n o t  needed before exiting sub

End Sub

Alternatively, to do exactly what you have above:

On Error Resume Next

Sheets("Report").Select

If Err = 0 Then

    CF code

Else

    Err.Clear

    Sheets("Data Input").Select

    If Err = 0 Then

        CF code

    Else

        Err.Clear

        Sheets("XML").Select

        If Err = 0 Then

            CF code

        End if

    End If

End If

On Error GoTo 0    ' n o t  needed before exiting sub

End Sub

And if you might have more than one of those worksheets in the same workbook in the future:

On Error Resume Next

Sheets("Report").Select

If Err = 0 Then

    CF code

End If

Err.Clear

Sheets("Data Input").Select

If Err = 0 Then

    CF code

End If

Err.Clear

Sheets("XML").Select

If Err = 0 Then

    CF code

End If

On Error GoTo 0    ' n o t  needed before exiting sub

End Sub

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2015-05-21T00:35:35+00:00

except for selecting the other workbook, the code you have is just about correct.

to make a label you do

err1:

so it is terminated with a colon.

On Error GoTo Err1

Sheets("Report").Select

CF code

.

Err1:

Resume Next

On Error GoTo Err2

Workbooks("Otherbook.xlsx").Activate

Sheets("Data Input").Select

CF code

.

Err2:

Resume Next

On Error GoTo Err3

Sheets("XML").Select

CF code

.

Err3:

Resume Next

End Sub

You could also do something like this (if the same commands are executed on each sheet)

Sub abc()

Dim v(1 To 3) As Worksheet

On Error Resume Next

  Set v(1) = Workbooks("Book1.xls").Worksheets("Report")

  Set v(2) = Workbooks("Book2.xls").Worksheets("Data Input")

  Set v(3) = Workbooks("Book2.xls").Worksheets("XML")

On Error GoTo 0

For i = 1 To 3

 If Not v(i) Is Nothing Then

   'CF

    MsgBox v(i).Range("A1").Address(0, 0, xlA1, True)

 End If

Next

End Sub

--

Regards,

Tom Ogilvy

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more