Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2022, 01:21 AM
Guessed's Avatar
Guessed Guessed 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
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,185
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

If you are adding another find element inside (brackets) then you will need to account for that shift in the replace with value. For example:


.Text = "(" & arrA(i) & ") ([A-Z0-9]{1,})"
changing to
.Text = "(" & arrA(i) & ")([ \^s])([A-Z0-9]{1,})"
means that you also need to change the replace with to
.Replacement.Text = "\1 <<\3>>"

You can include extra parts of the number by including the stops in the wildcard search
.Text = "(" & arrA(i) & ")([ \^s])([A-Z0-9.]{1,})"

However you may find that if the phrase ends with a stop immediately after the number then it will also be included in the highlighted range.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #2  
Old 08-05-2022, 01:57 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 Andrew, I did try the same thing before posting but it didn't work

Code:
.Text = "(" & arrA(i) & ")([ \^s])([A-Z0-9.]{1,})"
Maybe it needs an IF...Then - code below is taken from an auto numbering macro I have and wondering if it could be adapted for my highlight purpose perhaps

Code:
iNum = UBound(Split(.text, "."))
If IsNumeric(Split(.text, ".")(UBound(Split(.text, ".")))) Then iNum = iNum + 1
If iNum < 10 Then
.text = vbNullString
Reply With Quote
  #3  
Old 08-05-2022, 04:56 PM
Guessed's Avatar
Guessed Guessed 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
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,185
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

There are subtle changes to this code and it appears to work on my machine. Note the addition of the stop when applying the highlight - perhaps that was where your code was missing the element.
Code:
Sub HighlightNumbers()
  Dim StrFndA As String, StrFndB As String, i As Long, Rng As Range
  Dim arrA() As String, arrB() As String
  
  StrFndA = "Clause,Paragraph,Part,Schedule" 'highlight numbers after these words 'use initial caps
  StrFndB = "Minute,Hour,Day,Week,Month,Year,Working,Business" 'highlight numbers before these words  'use initial caps
  arrA = Split(StrFndA & "," & LCase(StrFndA), ",")   'both initial cap and lowercase versions of words
  arrB = Split(StrFndB & "," & LCase(StrFndB), ",")   'both initial cap and lowercase versions of words
  
  'Application.ScreenUpdating = False
  For Each Rng In ActiveDocument.StoryRanges
    With Rng
      Select Case .StoryType
        Case wdMainTextStory, wdFootnotesStory
          With Rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
            For i = 0 To UBound(arrA)
              .Text = "(" & arrA(i) & ")([ \^s])([A-Z0-9.]{1,})"
              .Replacement.Text = "\1 <<\3>>"
              .Execute Replace:=wdReplaceAll
              .Text = "(" & arrA(i) & "s)([ \^s])([A-Z0-9.]{1,})"
              .Execute Replace:=wdReplaceAll
            Next
            
            For i = 0 To UBound(arrB)
              .Text = "([0-9]{1,}) (" & arrB(i) & ")"
              .Replacement.Text = "<<\1>> \2"
              .Execute Replace:=wdReplaceAll
            Next
            
            Options.DefaultHighlightColorIndex = wdTurquoise
            .Replacement.Highlight = True
            .MatchWildcards = True
            .Text = "\<\<[A-Z0-9.]{1,}\>\>"
            .Replacement.Text = ""
            .Execute Replace:=wdReplaceAll
            
            .Replacement.ClearFormatting
            .MatchWildcards = False
            .Text = "<<"
            .Execute Replace:=wdReplaceAll
            .Text = ">>"
            .Execute Replace:=wdReplaceAll
            
          End With
        Case Else
      End Select
    End With
  Next Rng
  Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
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:58 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