Forum Discussion
Excel
Apr 05, 2021Iron Contributor
Question related to Autofilter using min and max value selection
Hello Everyone, Happy Easter to allππ In a excel report how can we auto filter one columns values by giving min and max value manually whenever we click macro. for example if i need to perform...
Subodh_Tiwari_sktneer
Apr 05, 2021Silver Contributor
You may try something like below. The following code will autofilter the column G as in the first line the constant variable called ColumnToFilter is set to 7. Change it as per your requirement.
Const columnToFilter As Long = 7 'This Will filter Column G (Units), change it as required
Public Sub AutoFilter()
Dim wsData As Worksheet
Dim userMin As Long
Dim userMax As Long
Application.ScreenUpdating = False
Set wsData = ThisWorkbook.Worksheets("AutoFilter")
userMin = Application.InputBox("Input the Min Criteria.", "Min Criteria!", Type:=1)
If userMin = 0 Then
MsgBox "You didn't input the Min Criteria. Please try again...", vbExclamation
Exit Sub
End If
userMax = Application.InputBox("Input the Max Criteria.", "Max Criteria!", Type:=1)
If userMax = 0 Then
MsgBox "You didn't input the Max Criteria. Please try again...", vbExclamation
Exit Sub
End If
If wsData.FilterMode Then wsData.ShowAllData
With wsData.Range("A1").CurrentRegion
.AutoFilter Field:=columnToFilter, Criteria1:=">=" & userMin, Operator:=xlAnd, Criteria2:="<=" & userMax
End With
Application.ScreenUpdating = True
End Sub
You may click the button called "Filter Data" on the AutoFilter Sheet to run the code.
Excel
Apr 05, 2021Iron Contributor
It worked! Thank you so much sirπ
Sir, in attached file, you have written -
Const columnToFilter As Long = 7 (only for G column)
What if we want to multiple columns, so what VBA code i should write?
Here is attached file
- Subodh_Tiwari_sktneerApr 05, 2021Silver ContributorWould you please elaborate what exactly you are trying to achieve?
What do you mean when you say that you want multiple columns? For how many columns you would like to ask criteria from user?- ExcelApr 05, 2021Iron Contributor
Const columnToFilter As Long = 7 (only for G column)
This code only for G column.
Suppose i want to autofilter Column F to Column G
So what we write in VBA code??
Please help...???
Here is a attached file.