Delete all paragraphs with a date

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Delete all paragraphs with a date

Post by gailb »

I have a document with paragraphs that are simply dates.

Sep 1, 2021, 9:26 PM
Aug 31, 2021, 10:05 PM
Aug 19, 2021, 5:58 PM
Aug 7, 2021, 3:46 PM
Jan 4, 2016, 7:08 PM
Sep 28, 2015, 4:14 PM

This date could be any month in the year and I'd just like to delete this paragraph. Again, the only thing in this paragraph is the date as shown about and it ends with a paragraph mark.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete all paragraphs with a date

Post by HansV »

Try this:

Code: Select all

Sub RemoveDates()
    Dim i As Long
    Dim par As Paragraph
    Dim txt As String
    Application.ScreenUpdating = False
    For i = ActiveDocument.Paragraphs.Count To 1 Step -1
        Set par = ActiveDocument.Paragraphs(i)
        txt = par.Range.Text
        txt = Left(txt, Len(txt) - 1)
        txt = Replace(txt, ",", "")
        If IsDate(txt) Then
            par.Range.Delete
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Delete all paragraphs with a date

Post by gailb »

That seems to have worked great. Thank you.