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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
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
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
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
 

Forum statistics

Threads
1,141,758
Messages
5,708,358
Members
421,566
Latest member
7Nabisco

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
Top