![]() |
#1
|
|||
|
|||
![]() Greeting, I have several word documents where I need to reformat those with commas in numerical value e.g. 2156 to 2,156. I was thinking about implementing find and replace but I am not confident enough. How can I get the job done? Any suggestions would be helpful. |
#2
|
|||
|
|||
![]()
I have applied the following way of dealing with the above problem. How can it be better??
Code:
Sub Comma() Selection.find.ClearFormatting With Selection.find .Text = "[0-9]{3,}" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With With Selection Do While .find.Execute = True If IsNumeric(.Range.Text) = True Then Select Case .Range.Characters.Count Case 4 If .Range.Text Like "20??" Then Else .Range.Characters(1).InsertAfter (",") .MoveRight wdCharacter, 1 End If Case 5 .Range.Characters(2).InsertAfter (",") .MoveRight wdCharacter, 1 Case 6 .Range.Characters(1).InsertAfter (",") .Range.Characters(4).InsertAfter (",") .MoveRight wdCharacter, 1 Case 7 .Range.Characters(2).InsertAfter (",") .Range.Characters(5).InsertAfter (",") .MoveRight wdCharacter, 1 Case 8 .Range.Characters(1).InsertAfter (",") .Range.Characters(4).InsertAfter (",") .Range.Characters(7).InsertAfter (",") .MoveRight wdCharacter, 1 Case 9 .Range.Characters(2).InsertAfter (",") .Range.Characters(5).InsertAfter (",") .Range.Characters(8).InsertAfter (",") .MoveRight wdCharacter, 1 Case 10 .Range.Characters(1).InsertAfter (",") .Range.Characters(4).InsertAfter (",") .Range.Characters(7).InsertAfter (",") .Range.Characters(10).InsertAfter (",") .MoveRight wdCharacter, 1 End Select End If Loop End With End Sub |
#3
|
||||
|
||||
![]()
It can be shorter
Code:
Sub Comma() Dim aRng As Range Set aRng = ActiveDocument.Range With aRng.Find .ClearFormatting .Text = "[0-9]{4,}" .Wrap = wdFindStop .MatchWildcards = True Do While .Execute = True If aRng.Text Like "20??" Then aRng.HighlightColorIndex = wdBrightGreen Else aRng.Text = Format(aRng.Text, "#,##0") End If aRng.Collapse Direction:=wdCollapseEnd Loop End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
![]()
Thank you for the reply Andrew.
|
![]() |
Tags |
comma, vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Robert Kay | Word VBA | 6 | 02-21-2018 04:41 PM |
List of figures with several labels (caption) - Table of Figures sorting | ibra_ca | Word | 2 | 10-11-2017 07:02 AM |
Automatic table of figures includes one of the figures, not just the caption - help! | sarahlt | Word | 1 | 09-28-2014 09:34 AM |
![]() |
sleake | Word | 8 | 09-15-2013 02:42 PM |
Plotting Numerical and Non-numerical Data Set | Ife | Excel | 0 | 04-23-2012 10:35 AM |