VBA AutoFilter

OX_2005

New Member
Joined
Feb 29, 2024
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Need a little help from the Excel Guru's out there.

I have a VBA AutoFilter set up and it works except for 1 small problem. If the auto filter returns with no data how can I with the code I have tell it after TBL.Range.AutoFilter Field:=12, Criteria1:="Term" to skip everything and start back after EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete?
LR = ThisWorkbook.Worksheets("Employees").Cells(Rows.Count, 1).End(xlUp).Row
TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
EE.Range("A2:L" & LR).Copy
TERM.Range("A1").End(xlDown).Offset(1).PasteSpecial (xlPasteValues)
Application.DisplayAlerts = False
EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete
TBL.AutoFilter.ShowAllData
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I think your reference to using TBL is using a ListObject but since you don't show what EE is I am a little confused.
See if this works for you:

Rich (BB code):
    LR = ThisWorkbook.Worksheets("Employees").Cells(Rows.Count, 1).End(xlUp).Row
    TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
    
    If TBL.Range.SpecialCells(xlCellTypeVisible).Rows.Address <> TBL.HeaderRowRange.Address Then
        EE.Range("A2:L" & LR).Copy
        TERM.Range("A1").End(xlDown).Offset(1).PasteSpecial (xlPasteValues)
        Application.DisplayAlerts = False
        EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete
    End If
    TBL.AutoFilter.ShowAllData
 
Upvote 0
I think your reference to using TBL is using a ListObject but since you don't show what EE is I am a little confused.
See if this works for you:

Rich (BB code):
    LR = ThisWorkbook.Worksheets("Employees").Cells(Rows.Count, 1).End(xlUp).Row
    TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
   
    If TBL.Range.SpecialCells(xlCellTypeVisible).Rows.Address <> TBL.HeaderRowRange.Address Then
        EE.Range("A2:L" & LR).Copy
        TERM.Range("A1").End(xlDown).Offset(1).PasteSpecial (xlPasteValues)
        Application.DisplayAlerts = False
        EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete
    End If
    TBL.AutoFilter.ShowAllData
Sorry TBL = Set TBL = EE.ListObjects("Employees")
 
Upvote 0
I know there should be an If and an End If but not sure what

Dim VT As Worksheet
Dim AT As Worksheet
Dim EE As Worksheet
Dim AB As Worksheet
Dim TERM As Worksheet
Dim LR As Long
Dim TBL As ListObject

Set VT = ThisWorkbook.Sheets("Vaca Tracker")
Set AT = ThisWorkbook.Sheets("Attend Tracker")
Set EE = ThisWorkbook.Sheets("Employees")
Set AB = ThisWorkbook.Sheets("Accrual Balances")
Set TERM = ThisWorkbook.Sheets("Term")
Set TBL = EE.ListObjects("Employees")


' Unprotecting Worksheets

VT.Unprotect
AT.Unprotect

' Not allowing screen to update

Application.ScreenUpdating = False

' Auto Filter Term & Delete

LR = ThisWorkbook.Worksheets("Employees").Cells(Rows.Count, 1).End(xlUp).Row
TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
If
EE.Range("A2:L" & LR).Copy
TERM.Range("A1").End(xlDown).Offset(1).PasteSpecial (xlPasteValues)
Application.DisplayAlerts = False
EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete
End If
TBL.AutoFilter.ShowAllData
 
Upvote 0
You don't seem to have used the If and End I provided. "Where" you have the If and End if fine but put my If line where you currently just have If.
If TBL.Range.SpecialCells(xlCellTypeVisible).Rows.Address <> TBL.HeaderRowRange.Address Then

PS: Please press the VBA code button and insert your code between the opening and closing code statements, it makes it much easier for others to read.
 
Upvote 0
So I added the IF statement you provided and right now the doc has nothing in the table listed as "Term" so it should be skipping all the copy and paste and delete, but its continuing to go threw those steps.

VBA Code:
Dim VT As Worksheet
Dim AT As Worksheet
Dim EE As Worksheet
Dim AB As Worksheet
Dim TERM As Worksheet
Dim LR As Long
Dim TBL As ListObject

Set VT = ThisWorkbook.Sheets("Vaca Tracker")
Set AT = ThisWorkbook.Sheets("Attend Tracker")
Set EE = ThisWorkbook.Sheets("Employees")
Set AB = ThisWorkbook.Sheets("Accrual Balances")
Set TERM = ThisWorkbook.Sheets("Term")
Set TBL = EE.ListObjects("Employees")


'   Unprotecting Worksheets

    VT.Unprotect
    AT.Unprotect

'   Not allowing screen to update

Application.ScreenUpdating = False

'   Auto Filter Term & Delete

LR = ThisWorkbook.Worksheets("Employees").Cells(Rows.Count, 1).End(xlUp).Row
TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
   If TBL.Range.SpecialCells(xlCellTypeVisible).Rows.Address <> TBL.HeaderRowRange.Address Then
   EE.Range("A2:L" & LR).Copy
   TERM.Range("A1").End(xlDown).Offset(1).PasteSpecial (xlPasteValues)
   Application.DisplayAlerts = False
   EE.Range("A2:L" & LR).SpecialCells(xlCellTypeVisible).Delete
   End If
   TBL.AutoFilter.ShowAllData
 
Upvote 0
Just out of curiosity, what does a message box say if you add one right after the filter
VBA Code:
TBL.Range.AutoFilter Field:=12, Criteria1:="Term"
   
   MsgBox EE.AutoFilter.Range.Columns(12).SpecialCells(xlCellTypeVisible).Cells.Count - 1
 
Upvote 0
It gives me a debug error

1710517717220.png
 
Upvote 0
If I change EE to TBL is get a count back of how many there are weather it be 1 or 0

VBA Code:
 MsgBox TBL.AutoFilter.Range.Columns(12).SpecialCells(xlCellTypeVisible).Cells.Count - 1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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