473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Searching Worksheets in Excel by Name

72
Hello,
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
  1. Private Sub cmdSearchName_Click()
  2. Dim ChemName As String
  3.     'assigns user's entry to the variable "ChemName"
  4.     ChemName = txtName.Value
  5.  
  6.     If ChemName = "" Then  'if user has not entered a name then display message
  7.         MsgBox ("Please enter a compound name")
  8.     Else  'display sheet with matching name
  9.         'Here is where I have problems
  10.         For Each ActiveWorkbook.name In Workbooks("BOOK1.XLS")
  11.             If ActiveWorkbook.name = ChemName Then
  12.                 Sheets(ChemName).Activate
  13.                 MsgBox ("" & ChemName & " has been found")
  14.                 frmNameSearch.Hide
  15.                 Exit For
  16.             End If
  17.         Next
  18.     End If
  19. End Sub
My logic is as follows:

[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!
May 28 '08 #1
3 1658
mforema
72
Hello,
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
  1. Private Sub cmdSearchName_Click()
  2. Dim ChemName As String
  3.     'assigns user's entry to the variable "ChemName"
  4.     ChemName = txtName.Value
  5.  
  6.     If ChemName = "" Then  'if user has not entered a name then display message
  7.         MsgBox ("Please enter a compound name")
  8.     Else  'display sheet with matching name
  9.         'Here is where I have problems
  10.         For Each ActiveWorkbook.name In Workbooks("BOOK1.XLS")
  11.             If ActiveWorkbook.name = ChemName Then
  12.                 Sheets(ChemName).Activate
  13.                 MsgBox ("" & ChemName & " has been found")
  14.                 frmNameSearch.Hide
  15.                 Exit For
  16.             End If
  17.         Next
  18.     End If
  19. End Sub
My logic is as follows:

[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!
I should also note that there will never be sheets that have the same name.

Thanks!
May 28 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. One way to do so, where you provide the name of the worksheet to search for as a string:

Expand|Select|Wrap|Line Numbers
  1. Public Sub MatchWorkSheetName(nametofind As String)
  2.     Dim objWorksheet As Worksheet
  3.     For Each objWorksheet In ActiveWorkbook.Worksheets
  4.         If objWorksheet.Name = nametofind Then
  5.             Worksheets(nametofind).Select
  6.             Exit Sub
  7.         End If
  8.     Next objWorksheet
  9.     ' if we get to here there has been no match
  10.     MsgBox "There are no sheets named " & nametofind & " in this workbook", vbExclamation
  11. End Sub
Even simpler if you want:

Expand|Select|Wrap|Line Numbers
  1. Public Sub MatchWorkSheetName(nametofind As String)
  2.     On Error GoTo NoMatch
  3.     Worksheets(nametofind).Select
  4.     Exit Sub
  5. NoMatch:    ' if we get to this error handler there has been no match
  6.     MsgBox "There are no sheets named " & nametofind & " in this workbook", vbExclamation
  7. End Sub
-Stewart
May 29 '08 #3
mforema
72
Hi. One way to do so, where you provide the name of the worksheet to search for as a string:

Expand|Select|Wrap|Line Numbers
  1. Public Sub MatchWorkSheetName(nametofind As String)
  2.     Dim objWorksheet As Worksheet
  3.     For Each objWorksheet In ActiveWorkbook.Worksheets
  4.         If objWorksheet.Name = nametofind Then
  5.             Worksheets(nametofind).Select
  6.             Exit Sub
  7.         End If
  8.     Next objWorksheet
  9.     ' if we get to here there has been no match
  10.     MsgBox "There are no sheets named " & nametofind & " in this workbook", vbExclamation
  11. End Sub
Even simpler if you want:

Expand|Select|Wrap|Line Numbers
  1. Public Sub MatchWorkSheetName(nametofind As String)
  2.     On Error GoTo NoMatch
  3.     Worksheets(nametofind).Select
  4.     Exit Sub
  5. NoMatch:    ' if we get to this error handler there has been no match
  6.     MsgBox "There are no sheets named " & nametofind & " in this workbook", vbExclamation
  7. End Sub
-Stewart
It worked!
Thanks Stewart!
May 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shawn | last post by:
I have the basics working as is described in KB199841. I currently have my ASP page loading into Excel by setting the ContentType of the page: <%response.ContentType="application/vnd.ms-excel"%> ...
2
by: JMCN | last post by:
hello, i have two worksheets that i need to import from a workbook that has a total of 5 worksheets. i tried to use this line of code but i run into an error message that it cannot find the...
1
by: mr_ocp | last post by:
Hi friends I need routines to create an excel file with worksheets for each customer and a text file as well for each customer, first worksheet would be a "Summary Report", here is the code that...
0
by: B | last post by:
Using Access2000, the sample code below is what I have been modifying and working on since the past week and I could not get it to work properly. What I wanted to accomplish: 1) read from a...
4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
1
by: J Daniel Melton | last post by:
Hello, I am using late binding in a managed VC++ .NET 2003 application. I used KB 302902 (for C#) as a starting point and converted it to managed C++. I built a managed class that is intantiated...
3
by: mike11d11 | last post by:
I was able to create three worksheets in my workbook, but when I go to add the 4th I get an Invalid Index error. I must be leaving something out to when adding 4 or more sheets. Thanks Dim...
1
by: MarkDotNet | last post by:
Hi I am trying to switch Excel worksheets in VBA. I get an error saying "subscript out of range". Please Help- Here is code (Fails on last 2 lines- Note that I ommited the recordset portion of...
1
by: rinkkunitr | last post by:
i have one asp page from where i am calling a xml page.from this xml page i m calling two asp pages to get an excel with two different worksheets. The variable containing a string, having some values...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.