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
 
Are you trying to copy all visible cells from the Action_Item_Log table to the other table?
Yes. Action_Item_Log table is the master table. I cloned your code 7 times. 7 different Macros for 7 different sheets that I wanted updated from the Action Item Log however only 2 out of the 7 are working and the rest are giving me that error and I cannot figure out why. They are all identical and the only things that differ are the names of the tables, the names of the sheets and what is being filtered
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Ok, try replacing
VBA Code:
.Parent.Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
with
VBA Code:
.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
 
Upvote 0
Ok, try replacing
VBA Code:
.Parent.Range("B3:G" & Cells(Rows.Count, 2).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
with
VBA Code:
.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy

Now they are all working! Thank you so much!!

What was causing the error? (If you don't mind me asking)
 
Upvote 0
Without being able to see your workbook, I'm not entirely sure.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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