macro autofilter skip if blank

phairplay

Active Member
Joined
Nov 2, 2011
Messages
260
I'm working on a macro that filters by a name then copies the data into a new workbook.

Code:
Sub copied()
'
' copied Macro
' Macro recorded 17/11/2011 by 
    Application.ScreenUpdating = False
    Dim Nm As String
    
    Set bk = ActiveWorkbook 'makes this the only active sheet so you can return to it
    Nm = InputBox("Enter the Name of the person to search and copy")
    Sheets("PL").Select
    Rows("1:1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=1, Criteria1:=Nm
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.copy
    Workbooks.Add
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWorkbook.SaveAs Filename:="C:\Users\" & _
        Application.UserName & _
        "\Desktop\Test.xlsx", _
                FileFormat:=xlWorkbookDefault, CreateBackup:=False
' now do the same for the next sheet
 bk.Activate 'returns the the active workbook
     Sheets("PI").Select
     Rows("1:1").Select
     Selection.AutoFilter
     Selection.AutoFilter Field:=1, Criteria1:=Nm
     Range("A2").Select
     Range(Selection, Selection.End(xlDown)).Select
     Range(Selection, Selection.End(xlToRight)).Select
     Selection.copy
     Windows("Test.xlsx").Activate
     Sheets("Sheet2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
' now do the same for the next sheet
            bk.Activate 'returns the the active workbook
     Sheets("UE").Select
     Rows("1:1").Select
     Selection.AutoFilter
     Selection.AutoFilter Field:=1, Criteria1:=Nm
     Range("A2").Select
     Range(Selection, Selection.End(xlDown)).Select
     Range(Selection, Selection.End(xlToRight)).Select
     Selection.copy
     Windows("Test.xlsx").Activate
      Sheets("Sheet3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
            Application.ScreenUpdating = True
End Sub

my problem is if one of the tabs = blank data excel seems to crash. Is there anyway to skip part of the macro if no data is found in the filter?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Untested so this may need some tweaking (additions in bold red):
Rich (BB code):
Sub copied()
'
' copied Macro
' Macro recorded 17/11/2011 by
    Application.ScreenUpdating = False
    Dim Nm As String
    
    Set bk = ActiveWorkbook 'makes this the only active sheet so you can return to it
    Nm = InputBox("Enter the Name of the person to search and copy")
    Sheets("PL").Select
    Rows("1:1").Select
    If WorksheetFunction.CountIf(Selection.Columns(1), Nm) = 0 Then GoTo nextSheet
    Selection.AutoFilter
    Selection.AutoFilter Field:=1, Criteria1:=Nm
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Workbooks.Add
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWorkbook.SaveAs Filename:="C:\Users\" & _
        Application.UserName & _
        "\Desktop\Test.xlsx", _
                FileFormat:=xlWorkbookDefault, CreateBackup:=False
' now do the same for the next sheet
nextSheet:
 bk.Activate 'returns the the active workbook
     Sheets("PI").Select
     Rows("1:1").Select
If WorksheetFunction.CountIf(Selection.Columns(1), Nm) = 0 Then GoTo nextSheet2
     Selection.AutoFilter
     Selection.AutoFilter Field:=1, Criteria1:=Nm
     Range("A2").Select
     Range(Selection, Selection.End(xlDown)).Select
     Range(Selection, Selection.End(xlToRight)).Select
     Selection.Copy
     Windows("Test.xlsx").Activate
     Sheets("Sheet2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
' now do the same for the next sheet
nextSheet2:
            bk.Activate 'returns the the active workbook
     Sheets("UE").Select
     Rows("1:1").Select
If WorksheetFunction.CountIf(Selection.Columns(1), Nm) = 0 Then exit sub
     Selection.AutoFilter
     Selection.AutoFilter Field:=1, Criteria1:=Nm
     Range("A2").Select
     Range(Selection, Selection.End(xlDown)).Select
     Range(Selection, Selection.End(xlToRight)).Select
     Selection.Copy
     Windows("Test.xlsx").Activate
      Sheets("Sheet3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
            Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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