I have a userform in Excel with a textbox and cmd button. I want to give the user the ability to search for a specific worksheet name within an Excel Workbook.
My code for the cmd button is as follows:
Expand|Select|Wrap|Line Numbers
- Private Sub cmdSearchName_Click()
- Dim ChemName As String
- 'assigns user's entry to the variable "ChemName"
- ChemName = txtName.Value
- If ChemName = "" Then 'if user has not entered a name then display message
- MsgBox ("Please enter a compound name")
- Else 'display sheet with matching name
- 'Here is where I have problems
- For Each ActiveWorkbook.name In Workbooks("BOOK1.XLS")
- If ActiveWorkbook.name = ChemName Then
- Sheets(ChemName).Activate
- MsgBox ("" & ChemName & " has been found")
- frmNameSearch.Hide
- Exit For
- End If
- Next
- End If
- End Sub
[PHP]For Each worksheet In Workbook
If worksheet's name = ChemName Then
Show the requested worksheet
Exit For
End if
Next[/PHP]
But I don't know how to write that in VBA. I attempted it, but I keep getting errors.
Can anyone help?
Thanks!