Range as filter criteria

Tablecloth98

New Member
Joined
Nov 15, 2023
Messages
24
Office Version
  1. 2021
Platform
  1. Windows
I'm trying to filter a column (E) by the values found in a range and to change all visible cells after that search to a new style ("Good"). For some reason it only seems to be picking up the first row in the range when applying the filter.

Does anyone know where I'm going wrong? (I've copied my code underneath)

Dim criteriaRange As Range

Dim criteriaArray As Variant

Set criteriaRange = Workbooks("Workbook.xlsm").Worksheets("Main")_
.Range("E2:E1048576").SpecialCells(xlCellTypeVisible)

criteriaArray = Application.Transpose(criteriaRange.Value)


ThisWorkbook.Sheets("Raw Data").Range("$A$1:$BA$1048576").AutoFilter Field:=5,_ Criteria1:=criteriaArray, Operator:=xlFilterValues, Criteria2:="<>"

ThisWorkbook.Sheets("Raw Data").Range("$A$2:$BA$1048576")_
.SpecialCells(xlCellTypeVisible).Style = "Good"
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Solved it. It didn't like me using SpecialCells(xlCellTypeVisible) when I set criteriaRange
 
Upvote 0
Solved it.
Good news. Thanks for letting us know. (y)

Care to show us how you did that (it might be helpful for others)?


It didn't like me using SpecialCells(xlCellTypeVisible) when I set criteriaRange
The problem wasn't specifically that you used xlCellTypeVisible it was that, with a filter applied, those visible cells will likely be in a number of disjoint 'areas'. When you try to put them into an array as you did, only the first area gets put in. So if your filter did only have one continuous range (area) of visible cells there would not have been a problem with the use of xlCellTypeVisible.
 
Upvote 0
That makes sense now that you say it. I used a bit of a workaround and copied the visible cells to a separate unfiltered sheet (putting them into a single range), then set criteriaRange equal to this new range.

It looks a little messy but gets the job done.
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

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