0

I'm looking to filter a column between values but those values are variables within my spreadsheet. Whenever I use this it filters all of the values and nothing is displayed. I'm assuming it's something with the syntax between the ">=" and "<=" in combination with the variables upper_limit and lower_limit but I can't quite figure it out.

Sub Filter_QB_Cost()
'
' Filter_QB_Cost Macro
'
    Dim upper_limit As Integer
    Dim lower_limit As Integer
    
    upper_limit = Application.Workbooks("Fantasy FB.xlsm").Worksheets("QB Selection").Range("D6")
    lower_limit = Application.Workbooks("Fantasy FB.xlsm").Worksheets("QB Selection").Range("D7")
'

    ActiveSheet.ListObjects("Table11").Range.AutoFilter Field:=5, Criteria1:= _
        ">=lower_limit", Operator:=xlAnd, Criteria2:="<=upper_limit"
End Sub
1
  • ">="&lower_limit note the use and location of the ampersand. Commented Nov 5, 2020 at 3:00

2 Answers 2

1

I haven't test it but you're missing some quotes and ampersands in order for the variables to be read.

Also some minor fixes with variable types and extra qualifiers not needed.

Sub Filter_QB_Cost()
'
' Filter_QB_Cost Macro
'
    Dim upper_limit As Long
    Dim lower_limit As Long
    
    upper_limit = Workbooks("Fantasy FB.xlsm").Worksheets("QB Selection").Range("D6").Value
    lower_limit = Workbooks("Fantasy FB.xlsm").Worksheets("QB Selection").Range("D7").Value
'

    ListObjects("Table11").Range.AutoFilter Field:=5, Criteria1:= _
        ">=" & lower_limit & "", Operator:=xlAnd, Criteria2:="<=" & upper_limit
End Sub

Let me know if it works

Sign up to request clarification or add additional context in comments.

1 Comment

Nailed it. Thank you, sir!
0

You can use & operator to concatenate strings and variables:

ActiveSheet.ListObjects("Table11").Range.AutoFilter Field:=5, Criteria1:= _
        ">="&lower_limit&"", Operator:=xlAnd, Criteria2:="<="&upper_limit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.