How to have VBA select only visible results of Adv Filter

sagain2k

Board Regular
Joined
Sep 8, 2002
Messages
94
I have a file that contains data with the top rows defined as a Criteria range for an Advanced filter. Here's the code to do the Adv Filter:

Range(Cells(6, 1), Cells(lastrowwithdata, 15)).AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range(Cells(1, 1), Cells(4, 15)), Unique:=False

(there's another procedure that yields the lastrowwithdata value).

How can I have VBA then select only the visible data result of the Advanced Filter? Running into problems since the row numbers of the result are of course mixed and out of sequence. I couldn't figure out how to have the above "Range" selected either by defining it as a variable, or somehow attaching the ".select" to it.

What I want to do is select the result of the Adv Filter, copy that data, and paste it again into another worksheet so the row numbers will be then in sequence.

Thanks for any suggestions!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Re: How to have VBA select only visible results of Adv Filte

Try declaring your filtered range as a variable, then filtering it, then selecting the visible cells in the range that is redefined by offsetting one row and resizing the range by one less row. Example:

Dim FilterRange As Range
Set FilterRange = Range(Cells(6, 1), Cells(lastrowwithdata, 15))
FilterRange.AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range(Cells(1, 1), Cells(4, 15)), Unique:=False
With FilterRange
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
End With
 
Upvote 0
I usually just assign the entire range of data to a variable. Then after you run the filter, you could use something like DataRange.SpecialCells(xlcelltypevisible).copy

This should only copy the visible rows in the area determined by "DataRange."

You can do some other things with SpecialCells as well. They're listed in the VBA help file.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,538
Members
449,316
Latest member
sravya

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