VBA Condition "If the sheet exists" Then...
Apprenti
-
ccm81 Posted messages 11033 Status Member -
ccm81 Posted messages 11033 Status Member -
Hello,
In my macro, I need to check if the sheet "NAME" exists before continuing my procedure, but I don't know how to write this in VBA. Does anyone have an answer?
Thank you!
Configuration: Windows XP / Internet Explorer 6.0
In my macro, I need to check if the sheet "NAME" exists before continuing my procedure, but I don't know how to write this in VBA. Does anyone have an answer?
Thank you!
Configuration: Windows XP / Internet Explorer 6.0
5 answers
-
Hello,
It is not necessary to loop to check if the sheet exists,
just do this:
Function Sheet_Exists(ByVal Sheet_Name As String) As Boolean Dim Sheet As Excel.Worksheet On Error GoTo Sheet_Absent_Error Set Sheet = ActiveWorkbook.Worksheets(Sheet_Name) On Error GoTo 0 Sheet_Exists= True Exit Function Sheet_Absent_Error: Sheet_Exists= False End Function
and to use it:
If Sheet_Exists("Sheet1") Thenor
If Not Sheet_Exists("Sheet1") Then