G
Greg Maxey
I was playing around with some code to format simple numbers like 1234 as 1,234 etc. I didn't want to process numbers like phone numbers 1-800-867-5309, or SSNs 123-45-6789 or serial numbers like 12345ERT56789WWQ1234 etc. I figured I could first find a group of 4 or more numbers and then check the preceeding and following characters and only process appropriate ranges.
In the code below, you will notice that I used Asc(oRng.Characters.Last.Next) and Asc(oRng.Characters.First.Previous) in my Select Case statements.
At first I tried just oRng.Characters.Last.Next and oRng.Characters.First.Previous and then usied Case Chr(7), Chr (9), etc., in the Case statements. To my dismay I discovered that it was not processing numbers found in a table cell. I am not sure, but I have heard that an end of cell marker is a composite Chr(7) and Chr(13). I just figured that .Next or .Previsous would be either a Chr(7) or Chr(13). When it didn't work I used: MsgBox Asc(oRng.Characters.Last.Next) in a Case Else statement and confirmed that "13" was returned. With this knowledge I changed the code accordingly.
I think it is interesting that it works one way and not the other when on the surface it looks the same. There must be some type of conversion going on with the Asc statement that I don't understand. If anyone cares to enlighten, I am all attention. Thanks.
Sub CommaFormatNumbers()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{4,})"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
Select Case Asc(oRng.Characters.Last.Next)
Case 7, 9, 10, 11, 13, 32
On Error GoTo Handler
Select Case Asc(oRng.Characters.First.Previous)
Case 7, 9, 10, 11, 13, 32
Proceed:
oRng = Format$(oRng, "#,##0")
oRng.Collapse wdCollapseEnd
Case Else
oRng.Collapse wdCollapseEnd
End Select
Case Else
'Do Nothing
End Select
Wend
End With
Exit Sub
Handler:
Resume Proceed
End Sub
In the code below, you will notice that I used Asc(oRng.Characters.Last.Next) and Asc(oRng.Characters.First.Previous) in my Select Case statements.
At first I tried just oRng.Characters.Last.Next and oRng.Characters.First.Previous and then usied Case Chr(7), Chr (9), etc., in the Case statements. To my dismay I discovered that it was not processing numbers found in a table cell. I am not sure, but I have heard that an end of cell marker is a composite Chr(7) and Chr(13). I just figured that .Next or .Previsous would be either a Chr(7) or Chr(13). When it didn't work I used: MsgBox Asc(oRng.Characters.Last.Next) in a Case Else statement and confirmed that "13" was returned. With this knowledge I changed the code accordingly.
I think it is interesting that it works one way and not the other when on the surface it looks the same. There must be some type of conversion going on with the Asc statement that I don't understand. If anyone cares to enlighten, I am all attention. Thanks.
Sub CommaFormatNumbers()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{4,})"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
Select Case Asc(oRng.Characters.Last.Next)
Case 7, 9, 10, 11, 13, 32
On Error GoTo Handler
Select Case Asc(oRng.Characters.First.Previous)
Case 7, 9, 10, 11, 13, 32
Proceed:
oRng = Format$(oRng, "#,##0")
oRng.Collapse wdCollapseEnd
Case Else
oRng.Collapse wdCollapseEnd
End Select
Case Else
'Do Nothing
End Select
Wend
End With
Exit Sub
Handler:
Resume Proceed
End Sub