Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-13-2022, 09:49 PM
Bikram Bikram is offline Inserting comma on numerical figures Windows 10 Inserting comma on numerical figures Office 2007
Advanced Beginner
Inserting comma on numerical figures
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default Inserting comma on numerical figures


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.
Reply With Quote
  #2  
Old 03-13-2022, 10:57 PM
Bikram Bikram is offline Inserting comma on numerical figures Windows 10 Inserting comma on numerical figures Office 2007
Advanced Beginner
Inserting comma on numerical figures
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 03-14-2022, 02:16 PM
Guessed's Avatar
Guessed Guessed is offline Inserting comma on numerical figures Windows 10 Inserting comma on numerical figures Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 03-14-2022, 05:59 PM
Bikram Bikram is offline Inserting comma on numerical figures Windows 10 Inserting comma on numerical figures Office 2007
Advanced Beginner
Inserting comma on numerical figures
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you for the reply Andrew.
Reply With Quote
Reply

Tags
comma, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting comma on numerical figures Code to find numerical string + space throughout document & replace them with Comma 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
Inserting comma on numerical figures Inserting Table of Figures Insists on Overwriting Existing 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft