Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2022, 01:19 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

Thank you Andrew and Macropod for taking the time to share your code, it is very much appreciated.

Macropod, I have just run DemoB and works perfectly if there are no cross reference fields within the document but appears to bug if there are cross reference fields.



Code:
.Start = .Words(2).Start
I have a second string (StrFndB) that should highlight digits before those words but not sure how to add that to your second code (DemoB).

Code:
Sub DemoB()
Application.ScreenUpdating = False
Dim StrFndA As String, StrFndB As String, i As Long, Rng As Range
StrFndA = "[Cc]lause,[Pp]aragraph,[Pp]art,[Ss]chedule" 'highlight numbers after these words
StrFndB = "[Mm]inute,[Hh]our,[Dd]ay,[Ww]eek,[Mm]onth,[Yy]ear,[Ww]orking,[Bb]usiness" 'highlight numbers before these words
For Each Rng In ActiveDocument.StoryRanges
  With Rng
    Select Case .StoryType
      Case wdMainTextStory, wdFootnotesStory
        For i = 0 To UBound(Split(StrFndA, ","))
          With .Duplicate
            With .Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Forward = True
              .Wrap = wdFindStop
              .MatchWildcards = True
              .text = Split(StrFndA, ",")(i) & "[s \^s]@[0-9.]{1,}"
            End With
            Do While .Find.Execute
              .Start = .Words(2).Start
              .HighlightColorIndex = wdTurquoise
              .Collapse wdCollapseEnd
            Loop
          End With
        Next
      Case Else
    End Select
  End With
Next Rng
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #2  
Old 08-08-2022, 06:55 AM
macropod's Avatar
macropod macropod 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
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,384
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Shelley Lou View Post
Macropod, I have just run DemoB and works perfectly if there are no cross reference fields within the document but appears to bug if there are cross reference fields.

Code:
.Start = .Words(2).Start
You could insert:
Code:
ActiveWindow.ActivePane.View.ShowFieldCodes = True
after:
Code:
Application.ScreenUpdating = False
and:
Code:
ActiveWindow.ActivePane.View.ShowFieldCodes = False
before:
Code:
Application.ScreenUpdating = True
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



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 08:05 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