mrMozambique
Board Regular
- Joined
- Mar 9, 2005
- Messages
- 97
I have some VBA code to filter a column and paste the autofilter results from the first three columns of the range into a second sheet. It then releases the autofilter and repeats the process for a second filtered column (same three columns of the filtered range are copied and pasted into a third sheet). If there are no results in the filtered range, it gives a small message box and continues on to the next procedure. This first procedure is called ANC. Everything seems to work fine in the first block of code.
However, if there are zero results from the second filtering procedure (called LD), it bypasses the message box and selects the entire range (even hidden rows) and the pastes them into my third sheet. The code is the same. I don't understand why the first one works and the second doesn't. Any ideas?
However, if there are zero results from the second filtering procedure (called LD), it bypasses the message box and selects the entire range (even hidden rows) and the pastes them into my third sheet. The code is the same. I don't understand why the first one works and the second doesn't. Any ideas?
Code:
Dim rng As Range
Dim rng2 As Range
'ANC
wsANC.AutoFilterMode = False
wsANC.Range("B5:D203").ClearContents
wsFACILITIES.Range("Clinical").AutoFilter Field:=6, Criteria1:="<>"
wsFACILITIES.Activate
wsFACILITIES.AutoFilter.Range.Select
With ActiveSheet.AutoFilter.Range
On Error Resume Next
Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
If rng2 Is Nothing Then
MsgBox "No data to copy"
Else
Set rng = ActiveSheet.AutoFilter.Range
rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count - 12).Copy
wsANC.Range("B5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wsFACILITIES.Range("Clinical").AutoFilter Field:=6
End If
wsFACILITIES.AutoFilterMode = False
'LD:
wsLD.AutoFilterMode = False
wsLD.Range("B5:D203").ClearContents
wsFACILITIES.Range("Clinical").AutoFilter Field:=7, Criteria1:="<>"
wsFACILITIES.Activate
wsFACILITIES.AutoFilter.Range.Select
With ActiveSheet.AutoFilter.Range
On Error Resume Next
Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
MsgBox rng2
End With
If rng2 Is Nothing Then
MsgBox "No data to copy"
Else
Set rng = ActiveSheet.AutoFilter.Range
rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count - 12).Copy
wsLD.Range("B5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wsFACILITIES.Range("Clinical").AutoFilter Field:=7
End If
wsFACILITIES.AutoFilterMode = False