Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-04-2022, 08:17 AM
Shelley Lou Shelley Lou is offline VBA Highlight numbers only after or before specific words help Windows 10 VBA Highlight numbers only after or before specific words help Office 2016
Expert
VBA Highlight numbers only after or before specific words help
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Highlight numbers only after or before specific words help

Hi, I am trying to put together a macro that highlights the numbers only after the words in StrFndA and before the words in StrFndB - any numbers in a cross reference field should not be highlighted. The words in StrFndA and StrFndB may be plural and the numbers to highlight in StrFndA will be 1., 1.1, 1.1.1 etc.



I've put some code together, it doesn't work yet as it's not complete and would appreciate some guidance as I'm a little stuck on how to bring it all together and also add in code for StrFndB.

Code:
Sub HighlightNumbers()
Application.ScreenUpdating = False
Dim StrFndA As String, StrFndB As String, i As Long, Rng As Range
StrFndA = "clause,paragraph,part,schedule" 'highlight numbers after these words
StrFndB = "minute,hour,day,week,month,year,working,business" 'highlight numbers before these words
For Each Rng In ActiveDocument.StoryRanges
  With Rng
    Select Case .StoryType
      Case wdMainTextStory, wdFootnotesStory
            With Rng.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .MatchCase = True
          .Wrap = wdFindContinue
          .MatchWholeWord = True
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Replacement.Highlight = True
          
          For i = 0 To UBound(Split(StrFndA, ","))
            .text = Split(StrFndA, ",")(i) & "[0-9]"
            .Execute Replace:=wdReplaceAll
            .text = Split(StrFndA, ",")(i) & "s" & "[0-9]"
            .Execute Replace:=wdReplaceAll
          Next
          Do While .Find.Found
          .Start = .Words.First.End
          If .text > .Paragraphs.First.Range.ListFormat.ListString Then
         .HighlightColorIndex = wdTurquoise
         End If
        .Find.Execute
         Loop
         
          Options.DefaultHighlightColorIndex = wdNoHighlight
          ActiveWindow.View.ShowFieldCodes = True 'Don't highlight cross refs already in fields
          .ClearFormatting
          .MatchWildcards = False
          .text = "^d"
          .Execute Replace:=wdReplaceAll
          ActiveWindow.View.ShowFieldCodes = False
        End With
      Case Else
    End Select
  End With
Next Rng
Application.ScreenUpdating = True
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Highlight numbers only after or before specific words help Highlight numbers after a specific word in numbered list liblikas90 Word VBA 3 02-27-2019 03:52 AM
How to highlight lines containing specific words SixStringSW Word VBA 4 06-03-2018 03:57 PM
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Search a cell that contains words and numbers and convert the numbers to metric Carchee Excel Programming 36 10-08-2014 03:16 PM
VBA Highlight numbers only after or before specific words help VBA to highlight words if used too much aolszewski Word VBA 3 11-23-2013 02:07 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:21 AM.


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