View Full Version : Solved: Remove Ordinal Suffix
georgeeasten
06-04-2011, 05:42 AM
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!):
Sub Ordinals()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Underline = wdUnderlineNone
With Selection.Find
.Text = "1st"
.Replacement.Text = "1"
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
.Text = "2nd"
.Replacement.Text = "2"
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
.Text = "3rd"
.Replacement.Text = "3"
.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.
Thanks in advance.
gmaxey
06-04-2011, 09:37 AM
I don't think intellect has anything to do with it. Exerience perhaps ;-)
Try:
Sub Ordinals2()
Dim SearchArray As Variant
Dim myRange As Range
Dim i As Long
Dim TermString As String
SearchArray = Array("st", "nd", "rd", "th")
Set myRange = ActiveDocument.Range
For i = 0 To UBound(SearchArray)
TermString = SearchArray(i)
With myRange.Find
.Text = "([0-9]{1,})" & TermString
.MatchWildcards = True
.Replacement.Text = "\1"
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
macropod
06-05-2011, 08:32 PM
Is there a reason a wildcard Find/Replace isn't sufficient?
Find = ([0-9])[dhnrst]{2}
Replace = \1
or, for a little more precision:
Find = ([0-9])[nrst][dht]
Replace = \1
gmaxey
06-05-2011, 08:39 PM
I step back to applaud with pleasure a man with more experience ;-)
georgeeasten
06-06-2011, 04:40 AM
Is there a reason a wildcard Find/Replace isn't sufficient?
Find = ([0-9])[dhnrst]{2}
Replace = \1
or, for a little more precision:
Find = ([0-9])[nrst][dht]
Replace = \1
Thank you, I now have the following:
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
Unfortunately I was unable to get the 'more precise' version to work.
COuld you just give a quick explanation of each part of the search and replace 'wildcards'.
Thanks again
macropod
06-06-2011, 04:07 PM
Hi George,
The '()' around part of a Find expression isolates it for re-use in the Replace expression
The '[]' around part of a Find expression tells it to look for any of the characters inside
The '0-9' inside the '[]' says to find any digit.
The \1 in the Replace expression says to re-insert the first Find expression bounded by '()'.
See Word's Help file for the full specs.
gmaxey
06-06-2011, 05:44 PM
Paul,
I was thinking on this some more and I hope this won't appear pedantic. I don't really know why the document has dates such as 4th June 2011 but I suppose it is possible to contain something like:
The party is 4th June 2011 at the corner of 22nd Steet and 3rd Avenue. If that is a possibility then I suppose one would have to check to see if "4th, 22nd and 3rd) are part of a date string. Something along this line perhaps:
Sub Ordinals2()
Dim myRange As Range
Dim oRngNext As Word.Range
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "([0-9])[dhnrst]{2}"
.MatchWildcards = True
.MatchWholeWord = True
While .Execute
Set oRngNext = myRange.Next(wdWord, 1)
Select Case True
Case IsDate(Trim("1 " & oRngNext.Text))
myRange.Text = Left(myRange.Text, Len(myRange.Text) - 2)
myRange.Collapse wdCollapseEnd
Case Else
'Ignore
End Select
Wend
End With
End Sub
macropod
06-06-2011, 06:04 PM
Hi Greg,
It can all be done with a single wildcard Find/Replace expression:
Find = ([0-9]{1,2})([dhnrst]{2})( [JFMASOND][anuryebchpilgstmov]{2,8} [12][0-9]{3}>)
Replace = \1\3
gmaxey
06-06-2011, 06:42 PM
Paul,
My head is spinning ;-). Thanks for the schooling.
macropod
06-06-2011, 07:08 PM
Y'ain't seen nothin' yet ...
georgeeasten
06-07-2011, 06:13 AM
It can all be done with a single wildcard Find/Replace expression:
Find = ([0-9]{1,2})([dhnrst]{2})( [JFMASOND][anuryebchpilgstmov]{2,8} [12][0-9]{3}>)
Replace = \1\3Gary - you are correct. The document may under certain circumstances contain further instances of ordinal numbers where the suffix will need to remain. I hadn't considered this, so thank you.
The dates are part of a chrnology of events and there may be reference addresses or the 1st, 2nd and 2rd instance of an event rather than a date.
I have updated everything and I have run tests - it all works!
Paul - thank you for your assistance and thank you for clarifying the wildcards.
Thread now marked resolved!
gmaxey
06-07-2011, 06:54 AM
Staying tuned.
Y'ain't seen nothin' yet ...
Frosty
06-07-2011, 09:34 AM
Paul- wow. Do you have these searches just laying around for rainy-day use, or you are simply a wildcard search savant?
I've read
http://word.mvps.org/faqs/general/usingwildcards.htm
(which I think is more helpful than the word help file on it), and I know I could eventually arrive at your wildcard solution, but my head starts pounding well before I get close.
Sorry to comment on all of the threads you're in, but I'm trying to learn as much as help :)
Bravo!
macropod
06-07-2011, 03:26 PM
Paul- wow. Do you have these searches just laying around for rainy-day use, or you are simply a wildcard search savant?
Hi Frosty,
Yes I do have some wildcard Find expressions lying around. The one in my last post is a simplified version of one that I posted a while ago in this forum (I think).
As for being a savant, I hope not (http://en.wikipedia.org/wiki/Savant_syndrome)
Frosty
06-07-2011, 05:11 PM
I meant more the http://dictionary.reference.com/browse/savant kind ;)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.