Filter and VBA using a dynamic range as a criteria

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hello,

I have this formula :
Code:
Selection.AutoFilter Field:=2, Criteria1:="<>" & Worksheets("Temp").Range("B2:B" & LastRowKTemp).Value

I can not make it work. If i hard code the criteria it is fine.
The value of the LastRowKTemp is 7, i checked that; It's fine.

When i use that formula, it is doing something
Selection.AutoFilter Field:=2, Criteria1:="<>" & Worksheets("Temp").Range("B2").Value
But as you can see the Criteria is not the one I want.

Then I started using the advanced filter :
Code:
   Range("A30:F171").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:F7"), Unique:=False
And the filter is working so I try to make it dynamic using that :
HTML:
     Range("A30:F171").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:F" & LastRowKTemp), Unique:=False

And then it is not working.

Please guide me to a solution.

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
OK. Forget it... I did not pay attention to the name of the Dynamic Range. It was only a spelling mistake. I Cant believe this amount of time i just spent on that.
Sorry for that... it must be the week end now :)
 
Upvote 0
Wait... no, It is really not working. Here is the line I am using
this one works :

Code:
 Sheets("Temp").Range("A30:F171").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:F" & LastRowKPlanTemp), Unique:=False
But not that one :

Code:
 Sheets("Temp").Range("A30:F171").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= "<>" & Range("A1:F" & LastRowKPlanTemp).Value, Unique:=False
 
Upvote 0
I am interested to see how this works. Currently I am running a similar situation however cannot seem to get it to work either.

Code:
    Dim rngCriteria As Excel.Range, rngValues As Excel.Range, FRows As Integer

    Set rngCriteria = Sheet5.Range("C28")


    Sheet2.Select
    With ActiveSheet
    FRows = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    Set rngValues = Range("A1:N" & FRows)
    Call rngValues.AutoFilter(Field:=10, Criteria1:=Split(Replace$(rngCriteria.Text, " ", ""), ","), Operator:=xlFilterValues)

If I change Set rngValues = Range("A1:N" & FRows) to Set rngValues = Range("A1:N500") then it will work. That however will not work as my range will very.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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