Hi Mr. Excel forums!
I am trying to make a search option with one of my excel workbooks. The user is supposed to type a search term into cell X12 and then click the search button (active x control). This prompts the code below to search through all the cells with data in column A and hide any rows which do not contain the user entered search term.
The code below works correctly, however it runs very slowly and isn't practical for users. It is so slow because it is cycling through each row and hiding/showing them one at a time. If anyone has any ideas for making this run more efficiently and quicker I'd really appreciate it!
One idea I had was to assign a value of hide or show to each row during the loop, then say something like...
With (all searchable rows)
Hide if RowValue = Hide
Show if RowValue = Show
End
I haven't been able to figure out a way to make the this idea work however...
Current working (but very slow) code:
Private Sub SEARCH_Click()
Dim ItemRow As Integer
Dim SearchBar As String
SearchBar = Range("X12")
'First searchable row
ItemRow = 18
'758 is last searchable row
Do While ItemRow < 759
If InStr(1, Cells(ItemRow, 1), SearchBar) Then
Rows(ItemRow).Hidden = False
Else
Rows(ItemRow).Hidden = True
End If
ItemRow = ItemRow + 1
Loop
End Sub
I am trying to make a search option with one of my excel workbooks. The user is supposed to type a search term into cell X12 and then click the search button (active x control). This prompts the code below to search through all the cells with data in column A and hide any rows which do not contain the user entered search term.
The code below works correctly, however it runs very slowly and isn't practical for users. It is so slow because it is cycling through each row and hiding/showing them one at a time. If anyone has any ideas for making this run more efficiently and quicker I'd really appreciate it!
One idea I had was to assign a value of hide or show to each row during the loop, then say something like...
With (all searchable rows)
Hide if RowValue = Hide
Show if RowValue = Show
End
I haven't been able to figure out a way to make the this idea work however...
Current working (but very slow) code:
Private Sub SEARCH_Click()
Dim ItemRow As Integer
Dim SearchBar As String
SearchBar = Range("X12")
'First searchable row
ItemRow = 18
'758 is last searchable row
Do While ItemRow < 759
If InStr(1, Cells(ItemRow, 1), SearchBar) Then
Rows(ItemRow).Hidden = False
Else
Rows(ItemRow).Hidden = True
End If
ItemRow = ItemRow + 1
Loop
End Sub