Find a sheet within a workbook (If/Then/Else)

  • Thread starter Thread starter Jacy Erdelt
  • Start date Start date
J

Jacy Erdelt

I have several sheets within a workbook. What I would like the program to do
is search through the sheet names and if there is a sheet name = to
txtName.Value then perform action, otherwise show a message telling the user
the input is not valid. I have already written the "then" and "else" portion
of this code, but I don't have the slightest idea how to have the program
search through the sheets to find the correct one (the "If"). Please help!
 
Maybe something like:

Sub getname()
Set txtName = Sheets("Sheet1").Range("A1")
v = txtName.Value
For Each sh In Sheets
If sh.Name = v Then
MsgBox ("FOUND IT!!")
Exit Sub
End If
Next
MsgBox ("did not find it.")
End Sub
 
You can do your test this way...

Dim TestName As String
....
On Error Resume Next
TestName = Sheets(txtName.Text).Name
If Err.Number = 0 Then
MsgBox "Exists"
Else
MsgBox "Doesn't exist"
End If
 

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