Checking if worksheet exsits

  • Thread starter Thread starter treasuresflemar
  • Start date Start date
T

treasuresflemar

I have
Dim rn as string ' contains name of worksheet
Dim rs as string , contains name of named range

How do I check to see if worksheet rn exists
and named range rs?
Currently trapping err.number but got to be a better way.

Thanks
 
Found = False
for each ws in Thisworkbook.sheets
if ws.name = rn then
Found = True
exit for
end if
next ws
if found = True then
' enter your code here

end if
Found = False
For Each nm In ActiveWorkbook.Names
If rs = nm.Name Then
found = True
Exit For
End If
Next
If found = True Then
'enter your code here
End If
 
On Error Resume Next
Set sh = Worksheets(rn)
On Error Goto 0
If Not sh Is Nothing Then
...

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Couple of simple ideas:

Sub CheckSheet()
On Error Resume Next
If Worksheets("Sheet2") Is Nothing Then
MsgBox "Sheet Does Not Exist"
Else
MsgBox "Sheet Already Exists"
End If
On Error GoTo 0
End Sub

Sub CheckNames()
Set wbn = ActiveWorkbook.Names
For r = 1 To wbn.Count
If wbn(r).Name = "Test" Then
MsgBox "Name Exists"
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top