VBA search function

Milksnatcher

New Member
Joined
Sep 21, 2009
Messages
7
Hi, I am a VBA beginner doing a project for work and am way out of my depth!!! I have a script which searches all sheets within the workbook for the partial names contained in cells b9-b19 of the "Results" spreadsheet. The findings are then displayed in a listbox in a userform which pops up. The entire cell contents of the match is shown, along with the worksheet name. The script is as follows:

Public Sub Locate(Name As String, Data As Range)
Dim rngFind As Range
Dim strFirstFind As String

With Data
Set rngFind = .Find(Name, LookIn:=xlValues)
If Not rngFind Is Nothing Then
strFirstFind = rngFind.Address
Do

If rngFind.Row > 1 Then
UserForm3.ListBox1.AddItem rngFind.Value
UserForm3.ListBox1.List(UserForm3.ListBox1.ListCount - 1, 1) = Data.Parent.Name
UserForm3.ListBox1.List(UserForm3.ListBox1.ListCount - 1, 2) = Data.Parent.Name & "!" & rngFind.Address
End If
Set rngFind = .FindNext(rngFind)
Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
End If
End With
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>

End Sub


Sub Searches()


Dim shtSearch As Worksheet
Dim lookout As String

lookout = b9

For Each shtSearch In ThisWorkbook.Worksheets
If Worksheets("Results").Range("b9") <> 0 Then Locate Worksheets("Results").Range("b9").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b10") <> 0 Then Locate Worksheets("Results").Range("b10").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b11") <> 0 Then Locate Worksheets("Results").Range("b11").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b12") <> 0 Then Locate Worksheets("Results").Range("b12").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b13") <> 0 Then Locate Worksheets("Results").Range("b13").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b14") <> 0 Then Locate Worksheets("Results").Range("b14").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b15") <> 0 Then Locate Worksheets("Results").Range("b15").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b16") <> 0 Then Locate Worksheets("Results").Range("b16").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b17") <> 0 Then Locate Worksheets("Results").Range("b17").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b18") <> 0 Then Locate Worksheets("Results").Range("b18").Text, shtSearch.Range("b2:C242")
If Worksheets("Results").Range("b19") <> 0 Then Locate Worksheets("Results").Range("b19").Text, shtSearch.Range("b2:C242")
Next
If UserForm3.ListBox1.ListCount = 0 Then
UserForm3.ListBox1.AddItem "No Match Found"
UserForm3.ListBox1.List(0, 1) = ""
UserForm3.ListBox1.List(0, 2) = ""
End If
UserForm3.Show
End Sub


This works well but there are three things that I can't work out! please help:

1) I want the entire cell contents aligned to the left of the listbox and the worksheet name aligned to the right. The box is wide enough but the worksheet name is overlapping some of the cell contents which needs to be visible. Is this possible?

2) When a match is found there is often a duplicate in the same sheet in a different column. Can I display only one occurence of the cell contents if they are identical?

3) This search is looking through all the sheets and therefore is returning matches from the "Results" worksheet (the search criteria itself). I do not want it to search this one sheet!!!


Help!!!!!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,215,737
Messages
6,126,573
Members
449,318
Latest member
Son Raphon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top