Make my macro ignore errors and keep going

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi Guys,
I need my macro to ignore any errors and keep going.

This is what I have. It copies data from two columns, on two seperate tables and puts them on another sheet.
Right now, if filtering produces no results to copy, it dies. I want it to just go 'oh well' if there is nothing to do, and keep going. :rolleyes:

Code:
Sub createlist()
Sheets.Add.Name = "List"
Sheets("List").Visible = True
    Sheets("sheet1").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:=
        "<>"
    Range("Table1[col1],table1[col4]").SpecialCells(xlCellTypeVisible).Select
    Selection.copy
    Sheets("List").Select
 Worksheets("List").Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Sheets("sheet2").Select
    ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=7, Criteria1:= "<>"
    Range("Table2[col1],table2[col4]").SpecialCells(xlCellTypeVisible).Select
    Selection.copy
    Sheets("List").Select
 Worksheets("List").Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
End Sub

Can it be made so that it moves on if it has nothing to copy?
 
Code:
Sub createlist()

    Dim ws As Worksheet

    On Error Resume Next
        Set ws = Sheets("List")
    On Error GoTo 0
    If ws Is Nothing Then
        Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
        ws.Name = "List"
    End If

    With Sheets("Sheet1")
        .ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:="<>"
        On Error Resume Next
        .Range("Table1[col1],table1[col4]").SpecialCells(xlCellTypeVisible).Copy _
            Destination:=ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        On Error GoTo 0
        .AutoFilterMode = False
    End With

    With Sheets("Sheet2")
        .ListObjects("Table2").Range.AutoFilter Field:=7, Criteria1:="<>"
        On Error Resume Next
        .Range("Table2[col1],table2[col4]").SpecialCells(xlCellTypeVisible).Copy _
            Destination:=ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        On Error GoTo 0
        .AutoFilterMode = False
    End With
    
End Sub
 
Upvote 0
Why not check if there is nothing to copy and skip that instead of skipping the error?

You should be able to do that with the same criteria your are using for the autofilter.
 
Upvote 0

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