Using Paste After Auto Filter

WireGuy8

New Member
Joined
Feb 7, 2022
Messages
18
I am working on a project that needs to have filtering of an excel sheet then pasting in another sheet. Everything works great if there is data in the range. If there is no data I get a 1004 Error.

The range C3:C502 is filtered and all data is hidden, so I do not need the copy and paste, just move on. If there is data, then need the copy/paste.

' Filter Source Data
sheet4.Range("B1").AutoFilter Field:=2, Criteria1:="Direct Embed"

' Apply Filter Direct Embed
sheet4.Select
Range("C3:C502").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
sheet3.Select
Range("D595").PasteSpecial Paste:=xlPasteValues
sheet4.Select
Range("R3:R502").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
sheet3.Select
Range("I595").PasteSpecial Paste:=xlPasteValues
Range("Q3:Q502").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
sheet3.Select
Range("F595").PasteSpecial Paste:=xlPasteValues
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You can add a condition to exit if the visible cell count exceeds 1 (assuming a header row).

VBA Code:
If Selection.SpecialCells(xlCellTypeVisible).Count > 1 Then
    'rest  of  code
Else
    Exit sub
End If
 
Upvote 0
Do you have any data in col A, or is it completely blank?
 
Upvote 0
The only problem is there are hundreds of other lines going on before and after this code. For that reason, I cannot exit the sub
 
Upvote 0
This code works great when there is data in cells C3:C502, when those cells are hidden by the filter since there is no data, I keep getting the error.
 
Upvote 0
The only problem is there are hundreds of other lines going on before and after this code. For that reason, I cannot exit the sub
You don't have to Exit - just use a GoTo statement to bypass the lines you don't want to execute.
 
Upvote 0
How about like
VBA Code:
With Sheet4
   .Range("B1").AutoFilter Field:=2, Criteria1:="Direct Embed"
   .AutoFilter.Range.Offset(1).Columns("C").Copy
   Sheet3.Range("D595").PasteSpecial xlPasteValues
   .AutoFilter.Range.Offset(1).Columns("R").Copy
   Sheet3.Range("I595").PasteSpecial xlPasteValues
   .AutoFilter.Range.Offset(1).Columns("Q").Copy
   Sheet3.Range("F595").PasteSpecial xlPasteValues
End With
 
Upvote 0
How about like
VBA Code:
With Sheet4
   .Range("B1").AutoFilter Field:=2, Criteria1:="Direct Embed"
   .AutoFilter.Range.Offset(1).Columns("C").Copy
   Sheet3.Range("D595").PasteSpecial xlPasteValues
   .AutoFilter.Range.Offset(1).Columns("R").Copy
   Sheet3.Range("I595").PasteSpecial xlPasteValues
   .AutoFilter.Range.Offset(1).Columns("Q").Copy
   Sheet3.Range("F595").PasteSpecial xlPasteValues
End With
That works perfect! Thank you!
 
Upvote 0
Glad we could help & thnaks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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