jbennett01
New Member
- Joined
- Apr 25, 2018
- Messages
- 12
- Office Version
- 2016
- Platform
- Windows
I am trying to create a search feature in a spreadsheet that will allow the user to enter search terms and have the macro find all the matching entries. Some of the search terms are specific, known quantities such as customer name, line of business abbreviation. Others are more free form because the data in those fields depends on the topic. The code is below and the Filter by Issue section is where I am having trouble. Any suggestions would be greatly appreciated.
VBA Code:
Sub Search_Feature()
' Macro to search changement management directives.
' Declare variables
Dim strLOB, strArea, strIssue As String
Dim lngTopFilteredRow, lngLastFilteredRow, lngCurrentRow, lngLastRow, lngSynopsis As Long
Dim intClientCount, intClientLen As Integer
' Capture search term(s)
strLOB = Range("B2").Value
strArea = Range("B3").Value
strIssue = Range("B4").Value
lngSynopsis = Range("B5").Value
' Get last row of Directives tab
Worksheets(2).Activate
lngLastRow = LastRowSearch("A")
If Range("A" & lngLastRow).Value = "" Then lngLastRow = lngLastRow - 1
' Filter by LOB, if applicable
If strLOB <> "" Then
ActiveSheet.Range("$A$1:$H$" & lngLastRow).AutoFilter Field:=4, Criteria1:=strLOB, Operator:=xlFilterValues
End If
' Filter by Area, if applicable
If strArea <> "" Then
ActiveSheet.Range("$A$1:$H$" & lngLastRow).AutoFilter Field:=5, Criteria1:=strArea, Operator:=xlFilterValues
End If
' Filter by Issue, if applicable
If strIssue <> "" Then
ActiveSheet.ListObjects("Table5").Range.AutoFilter Field:=6, Criteria1:=Array(strIssue), Operator:=xlFilterValues
End If
' Determine top and bottom filtered rows
lngTopFilteredRow = TopFilteredRowSearch("A")
lngLastFilteredRow = LastFilteredRowSearch("A")
' Remove all filtering and move to top of form.
Range("A2").Activate
ActiveSheet.ShowAllData
' Copy search results to Search Tab
Range("A" & lngTopFilteredRow & ":H" & lngLastFilteredRow).Select
Selection.Copy
Sheets("Search Tab").Select
Worksheets(1).Activate
Range("A9").Select
ActiveSheet.Paste
End Sub