How to check the sheet is still valid and exist ?

sthanawa

Board Regular
Joined
Jul 5, 2006
Messages
80
Hi All ,

I have one question , in my the excel workbook. They have many sheets inside there. How can I check ? Is the sheet name "Work10" still valid and exist in this workbook ? by VBA code.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
function sheetexists(sheetname as string) as boolean
dim s as object
for each s in sheets
if s.name = sheetname then
sheetexists = true
end if
next s
end function

Untested, but if you use this function, and pass it the sheet name you are looking for, this will return true if it exists, and false if it does not.

HTH

Patrick
 
Upvote 0
Hi,

another function
Code:
Sub yoursub()

Dim ShName As String

'other code
ShName = "Work10"
    If WksExists(ShName) Then
    'do your operations when it exists
    MsgBox "Worksheet exists", 48, "Title"
    Else
    'do your operations when it doesn't exists
    MsgBox "nope"
    End If
'code
End Sub

Function WksExists(wksName As Variant) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,020
Members
448,939
Latest member
Leon Leenders

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