Help with VBA code - wont Exit Sub if filtered table returns no results

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
I have tried the below 2 variations of code but I cannot seem to get the macro to Exit Sub when the filtered table returns no results.

Please advise what I am doing wrong?

Code Variation 1

VBA Code:
With Sheets("Chromium on Steels").ListObjects("Chromium_on_Steels_T2")
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
        End If
        End With

    Sheets("Action Item Log").Select
    ActiveSheet.ListObjects(1).AutoFilter.ShowAllData
    ActiveSheet.ListObjects("Action_Item_Log").Range.AutoFilter Field:=1, Criteria1 _
        :="Chromium on Steels"
If ActiveSheet.ListObjects("Action_Item_Log").Range.SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then
Sheets("Chromium on Steels").Select
    MsgBox "No Records Found"
    Exit Sub
Else

    Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets("Chromium on Steels").Select
    Range("Chromium_on_Steels_T2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A2").Select
End If

End Sub

Code Variation 2:

VBA Code:
With Sheets("Chromium on Steels").ListObjects("Chromium_on_Steels_T2")

If Not .DataBodyRange Is Nothing Then

.DataBodyRange.Delete

End If

End With



Sheets("Action Item Log").Select

ActiveSheet.ListObjects(1).AutoFilter.ShowAllData

ActiveSheet.ListObjects("Action_Item_Log").Range.AutoFilter Field:=1, Criteria1 _

:="Chromium on Steels"

If ActiveSheet.ListObjects("Action_Item_Log").Range.SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then

Sheets("Chromium on Steels").Select

MsgBox "No Records Found"

GoTo Quit:

Else



Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select

Selection.Copy

Sheets("Chromium on Steels").Select

Range("Chromium_on_Steels_T2").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

Range("A2").Select

End If

Quit:

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
VBA Code:
   With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Chromium on Steels"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).count = 1 Then
         MsgBox "No Records Found"
         Exit Sub
      End If
   End With
End Sub
 
Upvote 0
How about
VBA Code:
   With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Chromium on Steels"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).count = 1 Then
         MsgBox "No Records Found"
         Exit Sub
      End If
   End With
End Sub

How would I incorporate your code into my below code? If there are results I want it to select the specified range:

Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select


VBA Code:
With Sheets("Chromium on Steels").ListObjects("Chromium_Plating_on_Steels_T2")
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
        End If
        End With

    Sheets("Action Item Log").Select
    ActiveSheet.ListObjects(1).AutoFilter.ShowAllData
    ActiveSheet.ListObjects("Action_Item_Log").Range.AutoFilter Field:=1, Criteria1 _
        :="Chromium on Steels"
    Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets("Chromium on Steels").Select
    Range("Chromium_Plating_on_Steels_T2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A2").Select
 
Upvote 0
Like
VBA Code:
Sub willow()
   With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Chromium on Steels"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).count = 1 Then
         MsgBox "No Records Found"
         Exit Sub
      End If
      .Parent.Range("B3:G" & Cells(Rows.count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
   End With
   Range("Chromium_on_Steels_T2").Select
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
   Range("A2").Select
End Sub
 
Upvote 0
Solution
Like
VBA Code:
Sub willow()
   With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Chromium on Steels"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).count = 1 Then
         MsgBox "No Records Found"
         Exit Sub
      End If
      .Parent.Range("B3:G" & Cells(Rows.count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
   End With
   Range("Chromium_on_Steels_T2").Select
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
   Range("A2").Select
End Sub

I noticed you removed
With Sheets("Chromium on Steels").ListObjects("Chromium_on_Steels_T2")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
End With

Is there a reason why? I used this to clear Table: Chromium_on_Steels_T2 of all old data first. Does your code incorporate this somehow?
 
Upvote 0
You would need to put that at the start of the macro.
 
Upvote 0
You would need to put that at the start of the macro.
Now that I think of it, I would only want it cleared if there are filtered results....

I modified the code as follows and it appears to work. Thank you very much! ? ?

VBA Code:
        With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Chromium on Steels"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).Count = 1 Then
      Sheets("Action Item Log").ListObjects("Action_Item_Log").AutoFilter.ShowAllData
         MsgBox "No Records Found"
         Exit Sub
      End If
      With Sheets("Chromium on Steels").ListObjects("Chromium_on_Steels_T2")
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
        End If
        End With
      .Parent.Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
   End With
   Range("Chromium_on_Steels_T2").Select
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
      Application.CutCopyMode = False
      Sheets("Action Item Log").ListObjects("Action_Item_Log").AutoFilter.ShowAllData
   Range("A2").Select
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.

Sorry to bother you again, but I tried to apply the same code to another sheet however I am getting an error #1004 No cells were found on the .parent line. Is there something I am missing?

I thought I would only have to replace the Table and Sheet Names

VBA Code:
                With Sheets("Action Item Log").ListObjects("Action_Item_Log")
      .AutoFilter.ShowAllData
      .Range.AutoFilter Field:=1, Criteria1:="Copper Plating"
      If .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).Count = 1 Then
      Sheets("Action Item Log").ListObjects("Action_Item_Log").AutoFilter.ShowAllData
         MsgBox "No Records Found for Copper Plating Steels on Action Item Log"
         Exit Sub
      End If
      With Sheets("Copper Plating").ListObjects("Copper_Plating_T2")
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
        End If
        End With
      .Parent.Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
   End With
   Range("Copper_Plating_T2").Select
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
      Application.CutCopyMode = False
      Sheets("Action Item Log").ListObjects("Action_Item_Log").AutoFilter.ShowAllData
   Range("A2").Select

End Sub
 
Upvote 0
Are you trying to copy all visible cells from the Action_Item_Log table to the other table?
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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