Public datarange As Range
Public originalcell As Range
Public mytable As Range
Public count As Integer
Public originalrow As Integer
Public originalcolumn As Integer
Public lastrow As Long
Public keyword As String
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("C2")) Is Nothing And Range("C2").Value = keyword Then
Call FILTER_SHEET
End If
End Sub
Sub FILTER_SHEET()
ActiveWindow.FreezePanes = False
Dim searchArea As Range
Dim searchResults() As String
Call SET_VARIABLES
If keyword = "" Then
Call KILL_FILTER
Exit Sub
Else
Range("C4").AutoFilter Field:=3
keyword = "*" & Range("C2").Value & "*"
lastrow = Cells(Rows.count, "c").End(xlUp).row
Set mytable = Range("a4:h" & lastrow)
Set searchArea = Range("b4:h" & lastrow)
ReDim searchresults(1)
searchResults(1) = ""
For Each i In searchArea
If UCase(i.value) Like UCase(keyword) Then
ReDim Preserve searchResults(count)
searchResults(count) = Range("c" & i.row).Value
count = count+1
End If
Next i
lastrow = Cells(Rows.count, "c").End(xlUp).row
Set mytable = Range("a4:h" & lastrow)
mytable.Autofilter Field:=3, Criteria1:=searchResults, Operator:=xlFilterValues
Rows(5).Select
ActiveWindow.FreezePanes = True
Cells(originalrow, originalcolumn).Select
End If
End Sub
Sub KILL_FILTER()
ActiveWondow.FreezePanes = False
Cells(2,3).ClearContents
Call SET_VARIABLES
On Error GoTo Err
ActiveSheet.ShowAllData
Err:
Call REFREEZE_PANES
Cells(originalrow, originalcolumn).Select
End Sub
Sub SET_VARIABLES
originalrow = ActiveCell.row
originalcolumn = ActiveCell.Column
keyword = Cells(2,3).Value
Set originalcell = Cells(originalrow, originalcolumn)
Set datarange = Range(Cells(originalrow, "a"), Cells(originalrow, "h"))
End Sub
Sub REFREEZE_PANES()
Rows(5).Select
ActiveWindow.FreezePanes = True
ActiveSheet.AutoFilterMode = False
End Sub