VBA Form / Search with two criterias

Status
Not open for further replies.

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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Duplicate to: VBA Form / Search with two criterias

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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