Consulting

Results 1 to 2 of 2

Thread: Apply colour to numbers within brackets

  1. #1

    Apply colour to numbers within brackets

    Quote Originally Posted by macropod View Post
    As indicated in my reply to your cross-post (q.v.), if there isn't any content that might be affected outside tables, you could simply use:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = True
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Replacement.Text = "^&"
        .Text = "\[[0-9.]{1,}\]"
        .Replacement.Font.ColorIndex = wdBlue
        .Execute Replace:=wdReplaceAll
        .Text = "[\[\]]"
        .Replacement.Font.ColorIndex = wdAuto
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    Hi! I'm bringing this thread up because I found it when I was looking for a way to apply a font color to numbers inside brackets in my 400 page word document. The only difference is that I'm looking to apply the font color ONLY to numbers between [] brackets. Occasionally, I have something like this: [124; 125; 3456] and in that case, the font color should only be applied to the numbers, not the semicolons.

    Is there a way to modify the above code to accommodate this exception? Also, can I use RGB or HEX color codes? I'm not sure I understand the VB commands properly.

    Thank you for anyone who can help!

  2. #2
    It would have been better so start a new thread, however
    Sub Macro1()
    Dim oRng As Range
    Dim i As Long
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = True
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = True
            .Replacement.Text = "^&"
            .Text = "\[?@\]"    'find bracketed text
            Do While .Execute
                With oRng
                    .Start = .Start + 1    'omit the start bracket
                    .End = .End - 1    'omit the end bracket
                    For i = 1 To oRng.Characters.Count
                        If IsNumeric(oRng.Characters(i)) = True Then    'only format the numbers
                            'oRng.Characters(i).Font.Color = RGB(0, 0, 255)    'RGB blue
                            oRng.Characters(i).Font.Color = &HFF3F00    'Hex Blue
                        End If
                    Next i
                    .Collapse 0    'continue search from the end of the range
                End With
            Loop
        End With
        Set oRng = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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