If autofilter blank skip macro

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So I have been playing with If in my VBA, I have the below code that works perfect but I want to set an If function that will skip the section of code that opens and pastes data to my running file (I put lines sectioning the code I want to skip) if the autofilter does not have any data marked as "New"

Code:
Sub ExporttoDb()

        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
   Dim Wbk As Workbook
   Dim Mws As Worksheet, Nws As Worksheet
   
   Set Mws = ThisWorkbook.Sheets("Today")
   
          ' Filter table to new records only
        With Mws.ListObjects("TodayTable").DataBodyRange
        .AutoFilter 2, "New"
        
   '-----------------------------------------------------------------------------------------
   
        ' Set and open file
        Set Wbk = Workbooks.Open(Mws.Range("R1").Value)
        Set Nws = Wbk.Sheets("Data")
        
        ' Paste Data to file
        Mws.ListObjects("TodayTable").DataBodyRange.Offset(1).SpecialCells(xlVisible).Copy
    Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    
    ' Refresh's Pivot table data
   Worksheets("Pivot").Activate
   ActiveWorkbook.RefreshAll
      Wbk.Close True
      
    '----------------------------------------------------------------------------------------
      ' Clears Autofilter
     Mws.ShowAllData
     
      Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
   End With
End Sub
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try
Code:
Sub ExporttoDb()

        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
   Dim Wbk As Workbook
   Dim Mws As Worksheet, Nws As Worksheet
   
   Set Mws = ThisWorkbook.Sheets("Today")
   [COLOR=#ff0000]If Application.CountIf(Range("B:B"), "New") > 1 Then[/COLOR]
          ' Filter table to new records only
        With Mws.ListObjects("TodayTable").DataBodyRange
        .AutoFilter 2, "New"
        
   '-----------------------------------------------------------------------------------------
   
        ' Set and open file
        Set Wbk = Workbooks.Open(Mws.Range("R1").Value)
        Set Nws = Wbk.Sheets("Data")
        
        ' Paste Data to file
        Mws.ListObjects("TodayTable").DataBodyRange.Offset(1).SpecialCells(xlVisible).Copy
    Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    
    ' Refresh's Pivot table data
   Worksheets("Pivot").Activate
   ActiveWorkbook.RefreshAll
      Wbk.Close True
    '----------------------------------------------------------------------------------------
      ' Clears Autofilter
     Mws.ShowAllData
   [COLOR=#ff0000]End If[/COLOR]
      Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
   End With
End Sub
Change the Range to suit
 
Upvote 0
Thanks for the assist but I actually figured it out
Code:
Sub ExporttoDb()

      Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
   Dim Wbk As Workbook
   Dim Mws As Worksheet, Nws As Worksheet
   
   Set Mws = ThisWorkbook.Sheets("Today")
   
          ' Filter table to new records only
        With Mws.ListObjects("TodayTable").DataBodyRange
        .AutoFilter 2, "New"
          If Mws.ListObjects("TodayTable").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
   '-----------------------------------------------------------------------------------------
   
        ' Set and open file
        Set Wbk = Workbooks.Open(Mws.Range("R1").Value)
        Set Nws = Wbk.Sheets("Data")
        
        ' Paste Data to file
        Mws.ListObjects("TodayTable").DataBodyRange.Offset(1).SpecialCells(xlVisible).Copy
    Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    
    ' Refresh's Pivot table data
   Worksheets("Pivot").Activate
   ActiveWorkbook.RefreshAll
      Wbk.Close True
    '----------------------------------------------------------------------------------------
       End If
    ' Clears Autofilter
    Mws.ShowAllData
      Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
   End With
End Sub
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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