Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Worksheets

Status
Not open for further replies.

memberlogin

Technical User
May 20, 2003
19
GB
How can I find out if an Excel worksheet exists before running a macro so as to avoid an error?
 
On Error Resume Next
ThisWorkbook.Sheets("sheet_name").Activate
If Err <> 0 Then
MsgBox &quot;Sheet sheet_name does not exist&quot;
Exit Sub
End If
On Error GoTo 0

That should do it!

Good Luck,

Bryan Marble
IEWS
BAE Systems
 
I am sorry if I mislead you but the worksheets are in different Workbooks so the line:-
ThisWorkbook.Sheets(&quot;sheet_name&quot;).Activate
would not work!!
 
Code:
Option Explicit

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 19/06/2003 by Bryan Bayfield
'
Dim wkbIterate As Workbook, wksIterate As Worksheet, strList As String
strList = &quot;?&quot;
For Each wkbIterate In Application.Workbooks
    For Each wksIterate In wkbIterate.Sheets
        strList = strList & wksIterate.Name & &quot;?&quot;
        Next
    Next
Select Case InStr(strList, InputBox(&quot;Select a sheet name to search for&quot;))
    Case 0
        Debug.Print &quot;That sheet doesn't exist&quot;
    Case Else
        Debug.Print &quot;Sheet exists&quot;
    End Select
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top