Home » excel » vba – Macro Filter for a Range of Values/ Numbers in excel using Command Button

vba – Macro Filter for a Range of Values/ Numbers in excel using Command Button

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need support in creating a macro filter with command button.

I have numbers on column H2:H592

I would like to filter the values between a Range always. For examples 110 to 140

So I have two cells to enter the Range Excel F2 and F5.

I enter lower range as 110 and Upper Range as 140. I want the values between this range. I tried this by creating a command button macro and taking code for Automatic number filter from Record macro option. But doesnt work well. I mean not at all. Below is the code which used

ActiveSheet.Range("$H$5:$H$592").AutoFilter Field:=1, Criteria1:=">=Range("F2")", _
        Operator:=xlAnd, Criteria2:="<=Range("125")"

Thanks in Prior

How to&Answers:

You need to remove the Ranges from the quotes otherwies they are treated literally, and your second range was garbled.

ActiveSheet.Range("$H$5:$H$592").AutoFilter Field:=1, Criteria1:=">=" & Range("F2"), _
        Operator:=xlAnd, Criteria2:="<=" & Range("F5")

Including sheet references, amend to suit.

Sheets("Whatever").Range("$H$5:$H$592").AutoFilter Field:=1, Criteria1:=">=" & Sheets("Whatever").Range("F2"), _
        Operator:=xlAnd, Criteria2:="<=" & Sheets("Whatever").Range("F5")