PDA

View Full Version : Filter by User Input with multiple criteria?



jsabo
12-10-2012, 03:14 PM
Hello

I have a macro that collects user input and filters based on that. For example, column A contains the name of suppliers. Inputting "Company A" into the dialog box will delete ALL rows except those associated with "Company A".

Can anyone suggest how to allow input of multiple criteria? I want to have it so that users can filter out "Company A", "Company F", and "Company Y" and delete all other rows for instance. Here is the code I have so far which keeps only one input from the user:

Sub FilterSub()
Dim MyRange As Range, DelRange As Range, C As Range
Dim Cll As Excel.Range
Dim MatchString As String, SearchColumn As String, ActiveColumn As String
Dim FirstAddress As String, NullCheck As String
Dim AC

'Extract active column as text
AC = Split(ActiveCell.EntireColumn.Address(, False), ":")
ActiveColumn = AC(0)


On Error Resume Next
Set MyRange = ActiveSheet.Range("A2:A20000")
On Error GoTo 0

'If an invalid range is entered then exit
If MyRange Is Nothing Then Exit Sub

MatchString = InputBox("What are you filtering?", "Row Delete Code", ActiveCell.Value)
If MatchString = "" Then
NullCheck = InputBox("Do you really want to delete rows with empty cells?" & vbNewLine & vbNewLine & _
"Type Yes to do so, no to cancel", "Caution", "No")
If NullCheck <> "Yes" Then Exit Sub
End If

Application.ScreenUpdating = False


For Each Cll In MyRange.Cells
If InStr(1, Cll.Value, MatchString, vbTextCompare) = 0 Then
If DelRange Is Nothing Then Set DelRange = Cll Else Set DelRange = Union(DelRange, Cll)
End If
Next Cll
'If there are valid matches then delete the rows
If Not DelRange Is Nothing Then DelRange.EntireRow.Delete

Application.ScreenUpdating = True

End Sub


Thanks in advance!