Defining A Range For An Autofilter In VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to VBA code an autofilter on my worksheet (ws_salt)

Rich (BB code):
Private Sub uf2_create_Click()
    Dim f_range As Range
    With ws_salt
        .AutoFilterMode = False 'turn off autofilter if on
        f_lr = ws_sheet2.Cells(i, 11)
        f_ur = ws_sheet2.Cells(i, 12)
        last_col = .Range("A1").CurrentRegion.Columns.Count
        f_range = Range(.Cells(1, 1) & ":" & .Cells(1, last_col))
        f_range.AutoFilter field:=1, Criteria1:=">" & f_lr, Operator:=xlFilterValues
        f_range.AutoFilter field:=1, Criteria1:="<" & f_ur, Operator:=xlFilterValues
    
    End With

The autofilter I am trying to accomplish is to filter out all the rows of data, in which the value in column 1 of worksheet "ws_salt", a range of A: the last column of the worksheet with data in it (last_col), is greater than the lower value (f_lr) and less than the upper value (f_ur).

I am getting an error with the line in red. ("Method 'Range' of object '_Global' failed." Based on my unfamiliarity in applying autofilters through VBA, I will assume the remainder of this code is likely also chalked full of errors.

Any help will be greatly appreciated!
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try
Code:
f_range = Range(.Cells(1, 1), .Cells(1, last_col))
 
Upvote 0
Hey Fluff! Thanks you.
This has resulted in a different error ... "Object variable or With block variable not set"

Found the mistake ::
Rich (BB code):
set f_range = Range(.Cells(1, 1), .Cells(1, last_col))
 
Last edited:
Upvote 0
Put a dot in front of Range too and add Set at the start.
Code:
Set f_range = .Range(.Cells(1, 1), .Cells(1, last_col))
 
Last edited:
Upvote 0
OK! Thanks all, but as suspected, my filters are failing ...

Code:
         With f_range
            .AutoFilter field:=1, Criteria1:=">" & f_lr, Operator:=xlFilterValues, field:=1, Criteria2:="<" & f_ur, Operator:=xlFilterValues
         End With

"Wrong number of argument or invalid property assignment."
 
Upvote 0
Try
Code:
f_range.AutoFilter field:=1, Criteria1:=">" & f_lr, Operator:=xland,Criteria2:="<" & f_ur
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top