Results 1 to 6 of 6

Thread: [RESOLVED] Remove Ordinal Suffix

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Resolved [RESOLVED] Remove Ordinal Suffix

    Hello to those of superior intelect!

    I am working with a program which creates word documents with dates in the format 4th June 2011 (the suffix is not superscripted) and I need to be able to change the date format to 4 June 2011.

    My question is this; 'How do I remove the ordinal suffix?'

    I have the following code (it continues until 31) which does work but I was wondering if anyone knows of a tidier and/or more efficient way of solving it (I'm sure there is!):

    Code:
    Sub Ordinals()
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Font.Underline = wdUnderlineNone
        With Selection.Find
            .Text = "1st"
            .Text = "2nd"
            .Replacement.Text = "2"
            .Forward = True
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
        Selection.Find.Execute Replace:=wdReplaceAll
                
        Selection.HomeKey Unit:=wdStory
    
            .Text = "Engaged"
            .Replacement.Text = "Engaged -"
            .Forward = True
            .Format = False
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
        Selection.Find.Execute Replace:=wdReplaceAll
                    
        Selection.HomeKey Unit:=wdStory
    
            .Text = "% *"
            .Replacement.Text = "%"
            .Forward = True
            .Format = False
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
        Selection.Find.Execute Replace:=wdReplaceAll
        
        End With
            Selection.WholeStory
            Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
            Selection.HomeKey Unit:=wdStory
        
        
    End Sub
    Obviously bear in mind that there are words such as 'then' 'stick' and 'sand' where I do not want the th, st and nd removed.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Remove Ordinal Suffix

    Here is one way.

    Tested it with this text in the word document.

    This is an example with the words then stick and sand and the dates which are
    1st June 2010
    2nd June 2010
    10th June 2010
    HTH

    Sid

    Code Used

    Code:
    Sub Sample()
        Dim A(10) As String
    
        A(1) = "1st"
        A(2) = "2nd"
        A(3) = "3rd"
        A(4) = "4th"
        A(5) = "5th"
        A(6) = "6th"
        A(7) = "7th"
        A(8) = "8th"
        A(9) = "9th"
        A(10) = "0th"
    
        For i = 1 To 10
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = A(i)
                .Replacement.Text = Left(A(i), 1)
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
            Selection.WholeStory
        Next i
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Remove Ordinal Suffix

    here is another method you can try, i tested with sids sample text
    vb Code:
    1. Dim d As Range, w As Range
    2. Set d = ActiveDocument.Content
    3.  For Each w In d.Words
    4.     If Right(w.Text, 4) Like "#?? " Then
    5.         w = Left(w, Len(w) - 3) & " "
    6.     End If
    7.  Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Remove Ordinal Suffix

    Another way with a single complicated search string:
    Code:
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([0-9])([snrt][tdh]) (<[AFJMNSOD]*>) ([0-9]{4})"
            .Replacement.Text = "\1 \3 \4"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Remove Ordinal Suffix

    single complicated search string:
    neat
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Re: Remove Ordinal Suffix

    Thanks for all of the replies.

    @Koolsid - this doesnt wqork on 11th 12th and 13th presumably because they do not fit the 1st 2nd 3rd patern.

    @Westconn1 - this only works with numbers ending 1, 2, 7 and 8

    @anhn - this doesnt seem to work at all but I have made some amendments.

    I now have the following which works perfectly (and very efficiently) with all ordinal numbers 1st to 31st.

    Code:
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([0-9])[dhnrst]{2}"
            .Replacement.Text = "\1"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
    Thank you for your help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width