Young Grasshopper
Board Regular
- Joined
- Dec 9, 2022
- Messages
- 58
- Office Version
- 365
- 2016
- Platform
- 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;
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;
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