Apologies in advance if this is a long post, but I'll try and include as much detail as possible.
Just in case it may make a difference to any of the code
What I need help with is:- Coding the search function
- Coding the next/previous function of the search (each has its own button)
I have a Userform which will be used to capture incoming work opportunities. The work opportunities will be split over 5 worksheets, each with a corresponding table eg:- Worksheet (Tracker)/ Table_Main
- Worksheet (Go)/ Table_Go
- Worksheet(No Go)/ Table_NoGo
Everything so far is working well. But I've hit a bit of a snag when building the search function.
The user has the option of searching by Client or Opportunity name - Selected using a combobox, which in turn populates a second combobox with the available search names using the code below:
Private Sub SearchBy_Change()
Dim rngItems As Range
Dim oDictionary As Object
Dim tbl As ListObject
Dim ws As Worksheet
Select Case SearchBy.Text
Case "Client"
Me.SearchOptions.Clear
SearchOptions.Value = "Please Select Client"
For Each ws In ThisWorkbook.Worksheets
For Each tbl In ws.ListObjects
Set rngItems = tbl.ListColumns("Client").DataBodyRange
Set oDictionary = CreateObject("Scripting.Dictionary")
With Me.SearchOptions
For Each cel In rngItems
If oDictionary.exists(cel.Value) Then
'Do Nothing
Else
oDictionary.Add cel.Value, 0
.AddItem cel.Value
End If
Next cel
End With
Next tbl
Next ws
Case "Opportunity Name"
Me.SearchOptions.Clear
SearchOptions.Value = "Please Select Opportunity Name"
For Each ws In ThisWorkbook.Worksheets
For Each tbl In ws.ListObjects
Set rngItems = tbl.ListColumns("Opportunity Name").DataBodyRange
Set oDictionary = CreateObject("Scripting.Dictionary")
With Me.SearchOptions
For Each cel In rngItems
If oDictionary.exists(cel.Value) Then
'Do Nothing
Else
oDictionary.Add cel.Value, 0
.AddItem cel.Value
End If
Next cel
End With
Next tbl
Next ws
End Select
This makes sure that no duplicate entries appear in the combobox.
Once they've selected the Client or Opportunity name from the second combobox they then click the search button - which is where my issues begin.
I need the search button to accomplish several different things, not necessarily in this order:- search through a specific column in all the tables on all the sheets (depending on whether client or opportunity name has been previously selected)
- The code needs to identify on which sheet or in which table the matching entry was found
- Display several cell values from the found items row in several text boxes on the userform
Number two is important because not all the tables have the columns in the same order (Which wasn't my idea but came from the powers that be.) For example, The Decision column in the main tracker is Column L, in the other four tables it's K. This affects which cells from the table row populate which textboxes on the Userform.
I did start to build the code (in it's very early form - but couldn't progress as I didn't know how to accomplish 2 and therefore 3)
Sub Search()
Dim reference As String
reference = UserForm1.SearchOptions.Value
Dim aCell As Range
Dim tbl As ListObject
Dim ws As Worksheet
'Select Case SearchBy.Value
'Case "Opportunity Name"
For Each ws In ThisWorkbook.Worksheets
For Each tbl In ws.ListObjects
Set aCell = tbl.ListColumns("Opportunity Name").DataBodyRange.Find(What:=reference, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
UserForm1.TextBox12.Value = Offset(0, -7)
Next tbl
Next ws
I have included a version of the tracker with any confidential info removed, which will hopefully shed some light on the above.
Any help that anyone can give will be greatly appreciated, as I've been stuck on this for several days now.
Bookmarks