VBA Script that deletes all rows of data NOT specified in reference cells

PagsG

New Member
Joined
Apr 13, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Looking for help. The column below shows dates in the mm/dd/yyyy format, and can cover a range of time spanning several years. I want to write an Excel VBA script to identify the month specified in a different worksheet (Titled "REF"), and delete all rows that are not within that month/year.

For example, I'm measuring August, 2022 right now - and only want to keep the rows of data that begin with 8, and end with 2022.

1663083381292.png


REF worksheet example:

1663083708324.png


Thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Perhaps something like this?
I've assumed that the sheet with the rows to delete will already be selected when you run the code.

Note that I've had to test with UK format dates (Day/Month/Year) but as far as I can see it should work with any format as long as the dates are valid.
Excel Formula:
Option Explicit
Sub test()
Dim lRow As Long, refDate As String
lRow = Cells(Rows.Count, 35).End(xlUp).Row
With Worksheets("Ref")
    refDate = .Range("A2").Value & .Range("B2").Value
End With
Do Until lRow = 1
    If Not Format(Cells(lRow, 35), "myyyy") = refDate Then Rows(lRow).Delete
    lRow = lRow - 1
Loop

End Sub
 
Upvote 0
Solution
Perhaps something like this?
I've assumed that the sheet with the rows to delete will already be selected when you run the code.

Note that I've had to test with UK format dates (Day/Month/Year) but as far as I can see it should work with any format as long as the dates are valid.
Excel Formula:
Option Explicit
Sub test()
Dim lRow As Long, refDate As String
lRow = Cells(Rows.Count, 35).End(xlUp).Row
With Worksheets("Ref")
    refDate = .Range("A2").Value & .Range("B2").Value
End With
Do Until lRow = 1
    If Not Format(Cells(lRow, 35), "myyyy") = refDate Then Rows(lRow).Delete
    lRow = lRow - 1
Loop

End Sub
Thank you Jason,

This worked perfectly. I appreciate the help!
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,923
Members
449,094
Latest member
teemeren

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