?
Solved

Excel Form Control Button

Posted on 2011-04-26
13
Medium Priority
?
232 Views
Last Modified: 2012-06-21
I have an Excel workbook with several tabs.  Each tab contains an org chart.  The names on the org charts are in a text box.  I have this VBA code to perform a find on the workbook but once it finds the first person with either the entered last or first name it stops searching.  How to I alter my VBA code so that it finds all the people with the name, not just the first one?

Sub FindName()
    Dim rngName As Range
    Dim wks As Worksheet
    Dim tb As TextBox
    ' this is the input cell - adjust as need
    Set rngName = Sheets("Summary").Range("A15")
    For Each wks In ActiveWorkbook.Worksheets
        For Each tb In wks.TextBoxes
            If InStr(1, tb.Text, rngName.Value, vbTextCompare) > 0 Then
                wks.Activate
                Exit Sub
            End If
        Next tb
    Next wks
End Sub
0
Comment
Question by:barnescs
  • 5
  • 5
  • 2
12 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35469831
>>>> How to I alter my VBA code so that it finds all the people with the name, not just the first one?

The line in your code "Exit Sub" stop the macro after the first find. If you remove that then the cde will find the next instance.

Sid
0
 

Author Comment

by:barnescs
ID: 35469851
what about the programming after "Exit Sub"?  do i need to delete it also?
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35469867
No rest remain the same in the above code except "wks.Activate". In it's place extra code goes to do what you want to do with the found text.

Sid
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35469887
What exactly do you want to happen? If you remove the exit sub, it will just activate each sheet in turn and I don't see the point of that really?
0
 

Author Comment

by:barnescs
ID: 35469908
I want it to find the first person with that name and if that is not the person that you are looking for then go to the next one until you either find who you are looking for or you reach the last tab in the workbook.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35469929
Like I mentioned in ID: 35469867, you have to replace the "wks.Activate" with the relevant code. Do you have the code or do you want us to write it for you? :)

Sid
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 35469930
Something like:
Sub FindName()
    Dim rngName As Range
    Dim wks As Worksheet
    Dim tb As TextBox
    ' this is the input cell - adjust as need
    Set rngName = Sheets("Summary").Range("A15")
    For Each wks In ActiveWorkbook.Worksheets
        For Each tb In wks.TextBoxes
            If InStr(1, tb.Text, rngName.Value, vbTextCompare) > 0 Then
                wks.Activate
                If Msgbox("Found: " & tb.text & ". Continue search?", vbyesno) = vbno then Exit Sub
            End If
        Next tb
    Next wks
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35469957
What do you want to do when you find that name?

Sid
0
 

Author Comment

by:barnescs
ID: 35469962
Sub FindName()
    Dim rngName As Range
    Dim wks As Worksheet
    Dim tb As TextBox
    ' this is the input cell - adjust as need
    Set rngName = Sheets("Summary").Range("A15")
    For Each wks In ActiveWorkbook.Worksheets
        For Each tb In wks.TextBoxes
            If InStr(1, tb.Text, rngName.Value, vbTextCompare) > 0 Then
                wks.Activate
                If Msgbox("Found: " & tb.text & ". Continue search?", vbyesno) = vbno then Exit Sub
            End If
        Next tb
    Next wks
End Sub

This works PERFECT!!!!  THANKS!!!!!  :-)  :-)
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35469975
Thank you :)

Sid
0
 

Author Comment

by:barnescs
ID: 35470022
:-)
0
 

Author Comment

by:barnescs
ID: 35470179
Thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question