checking to see if worksheet exists

fluxcapacitr609

New Member
Joined
Feb 21, 2018
Messages
24
Code:
bCheck = Len(Sheets("Jan_2018").Name) 
If bCheck = True Then
    Set myCell = Sheets("Jan_2018")

Trying to see if a worksheet exists and if it does do something and if not skip, but it doesnt skip it always keeps writing on the previous sheet.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Maybe:
Code:
Sub TestForWorksheet()
    	Dim wsSheet As Worksheet
	On Error Resume Next
	Set wsSheet = Sheets("Jan_2018")
	On Error GoTo 0
	If Not wsSheet Is Nothing Then
    		MsgBox "I do exist"
	Else
    		MsgBox "I do NOT exist"
	End If	
End Sub
 
Upvote 0
At worst, bCheck will be 8 which is not the same as True. This function will check if a sheet exists:

Code:
Public Function SheetExists(sheetName As String) As Boolean

Dim testSheet As Worksheet

On Error Resume Next
Err.Clear
Set testSheet = Sheets(sheetName)
SheetExists = Not (testSheet Is Nothing)

End Function

Used like this:
Code:
If SheetExists("Jan_2018") Then
    ' Do something
End If

WBD
 
Upvote 0
Another option
Code:
Sub chk()
Dim bcheck As Boolean

bcheck = Evaluate("isref(Jan_2018!A1)")
If bcheck Then
   MsgBox True
Else
   MsgBox False
End If
End Sub
 
Upvote 0
Here is another UDF

Code:
Function WorksheetExists(sheetName as String, optional workbookName as String) As Boolean

    If workbookName = vbNullString then workbookName = ActiveWorkbook.Name

    On Error Resume Next
    WorksheetExists = (LCase(Workbooks(workbookName).Sheets(sheetName).Name) = LCase(sheetName))
    On Error Goto 0
End Function
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,540
Members
452,571
Latest member
MarExcelTips

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top