Prevent getting all rows if Autofilter criteria doesn't exist
Untested, it checks to see how many cells are visible in the first column of the
autofilter range. If it's 1, then only the headers are visible (no data is
shown).
Then it comes down one row and copies the visible data (all columns in the
filtered range) to its destination.
(Watch out for typos!)
Dim RngF as range
dim RngV as range
with sheets(wshname(x))
set rngf = .autofilter.range
end with
if rngf.columns(1).cells.specialcells(xlcelltypevisib le).cells.count = 1 then
'only headers are visible
else
with rngf
set rngv = .resize(.rows.count-1).offset(1,0) _
.cells.specialcells(xlcelltypevisible)
rngv.copy _
destination:=Sheets(WshName(a)).Range("A2")
end with
end with
RW wrote:
I am using the following code to cycle through criteria. However, if the data
does not match the criteria, I get all of the rows pasted to the new sheet.
How can this be prevented?
WshName, FCriteria and FCriteria2 are arrays. This code works when the data
contains the 2 criteria.
For a = y To z
Sheets(WshName(x)).Select
Range("B1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=2, Criteria1:=FCriteria(a)
Range("C1").Select
Selection.AutoFilter Field:=3, Criteria1:=FCriteria2(a)
Application.CutCopyMode = False
Range("A2:AV" & EndCell).Select
Selection.Copy
Sheets(WshName(a)).Select
Range("A2").Select
ActiveSheet.Paste
Range("A2").Select
Next a
--
Dave Peterson
|