Deleting series of dates

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
Each month I import data. I would like I macro that will enable me to select the month/s that I wish to retain in Col A and to delete all the other rows. For EG Column A muy contain 25/02/2011, 20/01/2011, 25/03/2011, 19/03/2011 (date in format dd/mm/yyyy)

Say I want to retain March (03), then in the example above the rows containing anything prior to 25/03/2011 must be deleted i.e the rows containing 25/03/2011, 19/03/2011 must be retained and all the other rows prior to 03 containing the date must be deleted

Your assistance will be most appreciated
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:
Code:
Sub RemoveMonths()
Dim lRw As Long, c As Range, mnthNum As String, delRws As Range, ct As Long
lRw = Range("A" & Rows.Count).End(xlUp).Row
mnthNum = InputBox("Enter a Month number from 1 to 12")
If mnthNum = "" Then Exit Sub
mnthNum = Val(mnthNum)
For Each c In Range("A1", "A" & lRw)
    If IsDate(c.Value) Then
        If Month(c.Value) <> mnthNum Then
            ct = ct + 1
            If delRws Is Nothing Then
                Set delRws = c
            Else
                Set delRws = Union(c, delRws)
            End If
        End If
    End If
Next c
If ct = 0 Then
    MsgBox "no rows with month number other than " & mnthNum & " found"
Else
    delRws.EntireRow.Delete
    MsgBox ct & " rows with dates not matching month number " & mnthNum & " were deleted"
End If
End Sub
 
Upvote 0
Hi JoeMo

Thanks very much for your help. The cose works perfectly

Regards

Howard
 
Upvote 0

Forum statistics

Threads
1,224,565
Messages
6,179,549
Members
452,927
Latest member
rows and columns

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