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

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
711
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
 

Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,240
Office Version
  1. 365
Platform
  1. Windows
Can you please post the entire code, as I supspect the line that gives the error is not included.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
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!
 
Solution

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
711
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,206
Messages
5,600,317
Members
414,376
Latest member
NickYOW

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
Top