VBA Form / Search with two criterias

Young Grasshopper

Board Regular
Joined
Dec 9, 2022
Messages
58
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I'm pretty new to VBA coding, and struggling to get VBA to understand what i want it to do..
I have a VBA form where i want to search for geotags, and if i search in "Locations" or in "Country Code" i get the results i want. But i want to do a search in both textboxes, f.ex: "Bergen" in location, and "NO" in Country code, and only get the results that match with both..

The code is a little massy still so Location textbox is "FName" and Country Code textbox is "LName".
"Table1" is just a table with column A:D, and Canonical Name (location) is in column A, and Country Code is in column D.
This is the code i have now, and don't really know "Where" to let Vba know i want it to search two values in two columns;

Skjermbilde 2022-12-09 100643.jpg


VBA Code:
Private Sub SearchBtn_Click()

    Dim SearchTerm As String
    Dim SearchColumn As String
    Dim RecordRange As Range
    Dim FirstAddress As String
    Dim FirstCell As Range
    Dim RowCount As Integer
   
    If FName.Value = "" And LName.Value = "" Then
   
        MsgBox "No search term specified", vbCritical + vbOKOnly
        Exit Sub
   
    End If
   
    If FName.Value <> "" Then
   
        SearchTerm = FName.Value
        SearchColumn = "Canonical Name"
       
    End If
   
    If LName.Value <> "" Then
   
        SearchTerm = LName.Value
        SearchColumn = "Country Code"
       
       
    End If
   
    Results.Clear

        With Range("Table1[" & SearchColumn & "]")

            Set RecordRange = .Find(SearchTerm, LookIn:=xlValues)

            If Not RecordRange Is Nothing Then

                FirstAddress = RecordRange.Address
                RowCount = 0

                Do

                    Set FirstCell = Range("A" & RecordRange.Row)
                   
                    Results.AddItem
                    Results.List(RowCount, 0) = FirstCell(1, 1)
                    Results.List(RowCount, 1) = FirstCell(1, 2)
                    Results.List(RowCount, 2) = FirstCell(1, 3)
                    Results.List(RowCount, 3) = FirstCell(1, 4)
                    RowCount = RowCount + 1
                   
                    Set RecordRange = .FindNext(RecordRange)

                    If RecordRange Is Nothing Then

                        Exit Sub

                    End If


                Loop While RecordRange.Address <> FirstAddress

            Else
           
                Results.AddItem
                Results.List(RowCount, 0) = "Nothing Found"
           
            End If

        End With

End Sub

I would appreciate any help:)
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi
you can modify your Range.Find method code to do what you want but may find it quicker to read the databodyrange of your table in to a variant array and then check against the required column elements (1 & 4) of the array for search criteria matches

make a backup of your workbook & then delete existing search codes & button as this will not be needed.

place these codes in the userform code page

VBA Code:
Dim tblTable    As ListObject
Dim ws          As Worksheet
Private Sub FName_Change()
    'Location.
    SearchGeoTags Me.Results, tblTable, Me.FName, Me.LName
End Sub

Private Sub LName_Change()
    'Country Code.
    SearchGeoTags Me.Results, tblTable, Me.FName, Me.LName
End Sub

Private Sub UserForm_Initialize()
    'worksheet object (change name as required)
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    'Table Object (change name as required)
    Set tblTable = ws.ListObjects("Table1")
   
    Me.Results.ColumnCount = tblTable.DataBodyRange.Columns.Count
End Sub

You will need to CHANGE the names of the worksheet & Table as required

Place following code in a standard module

VBA Code:
Sub SearchGeoTags(ByVal objListBox As Object, ByVal objTable As ListObject, ParamArray Search() As Variant)
    Dim SearchColumn(1 To 2) As Long, r  As Long, c As Long
    Dim arr                  As Variant
   
    'intialize array
    arr = objTable.DataBodyRange.Value
   
    'search columns
    SearchColumn(1) = 1        'Canonical Name
    SearchColumn(2) = 4        'Country Code
   
    With objListBox
        .Clear
        If Len(Search(0)) = 0 Then Exit Sub
        For r = 1 To UBound(arr, xlRows)
            If UCase(arr(r, SearchColumn(1))) Like UCase(Search(0)) & "*" And _
               UCase(arr(r, SearchColumn(2))) Like "*" & UCase(Search(1)) Then
                .AddItem arr(r, 1)
                For c = 2 To UBound(arr, xlColumns)
                    .List(.ListCount - 1, c - 1) = arr(r, c)
                Next c
            End If
        Next r
    End With
End Sub

code only lightly tested but should find as start typing entry in Location textbox, the listbox will begin to fill with likely matches
when done, enter text in country code - the listbox will clear until match(s) for both values are found

Hope Helpful

Dave
 
Upvote 0
Solution
Thanks dave,
The "problem" is that there is almost 150k lines of data, and from earlier tests, Excel just craches when trying to quickly read trough it.
That's why i added the search function.
 
Upvote 0
The "problem" is that there is almost 150k lines of data,

Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive?
And then share the link here.
Just include about 1K rows of data.
 
Upvote 0
Thanks dave,
The "problem" is that there is almost 150k lines of data, and from earlier tests, Excel just craches when trying to quickly read trough it.
curious not an issue I have come across when using arrays but will have a look at your workbook link & see if can adapt suggestion to resolve it.

meanwhile,
try using update to code which requires an exact match (no filtering) & see if this helps

VBA Code:
Sub SearchGeoTags(ByVal objListBox As Object, ByVal objTable As ListObject, ParamArray Search() As Variant)
    Dim SearchColumn(1 To 2) As Long, r  As Long, c As Long
    Dim arr                  As Variant
    
    'intialize array
    arr = objTable.DataBodyRange.Value
    
    'search columns
    SearchColumn(1) = 1        'Canonical Name
    SearchColumn(2) = 4        'Country Code
    
    With objListBox
        .Clear
        If Len(Search(0)) = 0 Then Exit Sub
        For r = 1 To UBound(arr, xlRows)
            If UCase(arr(r, SearchColumn(1))) = UCase(Search(0)) And _
               UCase(arr(r, SearchColumn(2))) = UCase(Search(1)) Then
                .AddItem arr(r, 1)
                For c = 2 To UBound(arr, xlColumns)
                    .List(.ListCount - 1, c - 1) = arr(r, c)
                Next c
            End If
        Next r
    End With
End Sub

Dave
 
Last edited:
Upvote 0
I still can't access your workbook. Probably you have different setting, try:
On your google drive:
Right click the file you want to share > Click Get Link > don't choose "Restricted", choose "Anyone with link" instead > Click Copy Link > now you get the link, just paste the link here.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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