

To test the above macro, we need to create a sheet name “Main”.Similarly, we can have a simple IF loop which checks if the sheet exists and performs certain actions thereafter. If Sheet does not exist we will get the following message box: WorksheetExists2 = (.Sheets(WorksheetName).Name = WorksheetName)Īfter running the macro “FindSheet”, we will get the following message box if sheet exists: If wb Is Nothing Then Set wb = ThisWorkbook
#Visual basic file exists code
To check, we will copy the following code in the standard module:įunction WorksheetExists2(WorksheetName As String, Optional wb As Workbook) As Boolean We can have a normal subroutine which is calling a UDF and, if the specified sheet is found, the message box will display, ‘sheet exist’ if not found, then msgbox pops up, ‘sheet not found’. Second Option: Sub routine through message box The loop goes from the 1st sheet to the next until all the sheets have been checked. Otherwise, WorksheetExists = False is returned back to the main macro. If it does, WorksheetExists is True, and we can exit the function. Then If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) ThenĬhecks if the Sheet name is matching the Sheet name passed from the main macro. If you need to change it as per your code, you may.įor Each Sht In ThisWorkbook.Worksheets and Next Sht are the starting and ending parts of the loop respectively.

This function takes the value for “WorksheetName” from the macro which performs other activities. In the above image, “MasterSheet” does not exist in our sample workbook hence, formula has given answer as False.In order to check, we will use UDF in cell B2 as.If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then Copy the below code in the standard moduleįunction WorksheetExists(ByVal WorksheetName As String) As Boolean.To find if a specific sheet exists, we need to follow the below steps to launch VB editor We will cover the following ways in this article:įollowing snapshot contains few sheets names & we will check if the names of sheet in column A exist. There are multiple ways of checking if a worksheet exists. We can do this easily using a function / macro. There could be a time when you have to check if a worksheet, which you have create or deleted in a workbook in a VBA macro / code, exists.
