Seeking method to skip a code line if filtered data is empty...

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
I have this small bit of code which is part of a much larger project. My 'source' book includes client names in column C. The code opens a workbook, then filters by a Client, copies the filtered data, then pastes it into the 'destination' book. The following line results in an error message when the client is not included in the source book.

VBA Code:
ActiveSheet.Range("C3:AGC" & Last_C).AutoFilter Field:=1, Criteria1:=TheClient
Range("C3:AGP" & Last_C).Copy

I need some method to skip forward in the code, without the error message, which reads: Runtime error 1004: No cells were found.

Thanks in advance.

Jim
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Can you please post the entire code, as I supspect the line that gives the error is not included.
 
Upvote 0
First we try to assign the visible cells to an object variable. Then, if nothing gets assigned to the variable, we know that no cells were visible. Maybe something like this...

VBA Code:
Dim filter_range As Range

With ActiveSheet.Range("C3:AGP" & Last_C)
    .AutoFilter Field:=1, Criteria1:=TheClient
    On Error Resume Next
    Set filter_range = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not filter_range Is Nothing Then
        Range("C3:AGP" & Last_C).Copy Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1") 'change the destination worksheet accordingly
    End If
    .AutoFilter
End With

Hope this helps!
 
Upvote 0
Solution
First we try to assign the visible cells to an object variable. Then, if nothing gets assigned to the variable, we know that no cells were visible. Maybe something like this...

VBA Code:
Dim filter_range As Range

With ActiveSheet.Range("C3:AGP" & Last_C)
    .AutoFilter Field:=1, Criteria1:=TheClient
    On Error Resume Next
    Set filter_range = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not filter_range Is Nothing Then
        Range("C3:AGP" & Last_C).Copy Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1") 'change the destination worksheet accordingly
    End If
    .AutoFilter
End With

Hope this helps!


Thanks! This worked with a minor modification.

Jim
 
Upvote 0
That's great, and thanks for the feedback!

Cheers!
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,915
Members
448,532
Latest member
9Kimo3

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