Excel VBA to delete specific Year-Month on two separate tabs

bthumble

Board Regular
Joined
Dec 18, 2007
Messages
231
Hello,

Need help with VBA code that will delete a specific Year-Month on two specific sheets based on a prompt for user input.

Sheet1 has Year-Month in column HC and Sheet2 has Year-Month in column BE. Would like the VBA to prompt the user to enter the month-year to delete. For example enter 2023-5 to delete May 2023 rows.

Thanks for your time an deffort.
 
Is there a way to leave the filters there>
Try delete this line:
.AutoFilterMode = False

VBA Code:
Sub DeleteRows()
  Dim ym As Variant
  
  ym = InputBox("Enter year-month, ex: 2023-5")
  If ym = "" Then Exit Sub
  
  With Sheets("Sheet1")
    .Range("HC1", .Range("HC" & Rows.Count).End(3)).AutoFilter 1, ym
    .AutoFilter.Range.Offset(1).EntireRow.Delete
   
  End With
  With Sheets("Sheet2")
    .Range("BE1", .Range("BE" & Rows.Count).End(3)).AutoFilter 1, ym
    .AutoFilter.Range.Offset(1).EntireRow.Delete
   
  End With
End Sub
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,215,831
Messages
6,127,147
Members
449,364
Latest member
AlienSx

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