Delete records across all sheets based on multiple criteria [user input method]

Manerlao

Board Regular
Joined
Apr 14, 2020
Messages
56
Office Version
  1. 2019
Platform
  1. Windows
Dear Excel, VBA community!

I have an issue with my VBA code and hoping someone could very kindly assist!
The code below is meant to delete multiple records across all excel sheets based on two or more criteria. So far I have made it for just two criteria where the user inputs the product type and date.
However, the VBA code fails for the section which is highlighted below. Any advice and help would be great, thank you all very much!

Best,
Manerlao

Section which has the error:

VBA Code:
  '1. Apply Filter
  For Each ws In ActiveWorkbook.Sheets
  ws.Range("A2:XX1000000").AutoFilter Field:=1, Criteria1:=tCriteria
  ws.Range("A2:XX1000000").AutoFilter Field:=3, Criteria1:=sCriteria


Full code:
VBA Code:
Sub DeleteSelectedRws()
'Delete records based on date and product
'Display Yes/No message prompt before deleting rows

Dim ws As Worksheet
Dim lRows As Long
Dim vbAnswer As VbMsgBoxResult
Dim sCriteria As Variant
Dim tCriteria As Variant

  'Set reference to the sheet and Table.
  Set ws = ActiveSheet
  ws.Activate 'Activate sheet that Table is on.
  
  'Clear any existing filters
  
  'Ask user for input
  sCriteria = Application.InputBox(Prompt:="Please enter the filter criteria for the PRODUCT column." _
                                    & vbNewLine & "Leave the box empty to filter for blanks.", _
                                    Title:="Filter PRODUCT (1 of 2)", _
                                    Type:=2)
                                    
  tCriteria = Application.InputBox(Prompt:="Please enter the filter criteria for the DATE column." _
                                    & vbNewLine & "Leave the box empty to filter for blanks.", _
                                    Title:="Filter DATE (2 of 2)", _
                                    Type:=2)
  
  'Exit if user presses Cancel button
  If sCriteria = False And tCriteria = False Then Exit Sub
  
  '1. Apply Filter
  For Each ws In ActiveWorkbook.Sheets
  ws.Range("A2:XX1000000").AutoFilter Field:=1, Criteria1:=tCriteria
  ws.Range("A2:XX1000000").AutoFilter Field:=3, Criteria1:=sCriteria

  
  'Count Rows & display message
  On Error Resume Next
    lRows = WorksheetFunction.Subtotal(103, ws.Range("A2:A1000000").SpecialCells(xlCellTypeVisible))
  On Error GoTo 0
  Next ws
  
  vbAnswer = MsgBox(lRows & " Rows will be deleted.  Do you want to continue?", vbYesNo, "Delete Rows Macro")
  
  If vbAnswer = vbYes Then
    
    'Delete Rows
    Application.DisplayAlerts = False
      ws.Range("A2:XX1000000").SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
  
    'Clear Filter
    For Each ws In ActiveWorkbook.Sheets
    ws.Activate
    ws.ShowAllData
    ws.Range("A1").Select
    Next ws
    Else
    For Each ws In ActiveWorkbook.Sheets
    ws.Activate
    ws.ShowAllData
    ws.Range("A1").Select
    Next ws
    End If

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
VBA Code:
  For Each ws In ActiveWorkbook.Sheets
  ws.Range("A2").AutoFilter Field:=1, Criteria1:=tCriteria
  ws.Range("A2").AutoFilter Field:=3, Criteria1:=sCriteria
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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