Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2023, 02:15 PM
jmdonhauser jmdonhauser is offline Macro to search word document for a several specific numbers and highlight them if they exist Windows 11 Macro to search word document for a several specific numbers and highlight them if they exist Office 2016
Novice
Macro to search word document for a several specific numbers and highlight them if they exist
 
Join Date: Sep 2023
Posts: 3
jmdonhauser is on a distinguished road
Default Macro to search word document for a several specific numbers and highlight them if they exist

Hi, I am kind of new to writing VBA macros and having difficulty figuring this one out. I am trying to create a macro that searches my word document for several specific numbers and highlight them all if they exist in the document. I know how to do a basic search and highlight a single number, but not several. I am not sure if there's a way to modify a basic search to find several numbers at once or if I truly need a macro. Let's say for example I want to search for 25.222, 25.2666, 25.223 all at once and highlight any instances of them if they exist in the document. Sorry, this might be a noob question. Thanks for any help. It's appreciated.



Jeff
Reply With Quote
  #2  
Old 09-21-2023, 11:21 PM
gmayor's Avatar
gmayor gmayor is offline Macro to search word document for a several specific numbers and highlight them if they exist Windows 10 Macro to search word document for a several specific numbers and highlight them if they exist Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It may not be possible to do this in a single pass, without knowing what else is in the document, so I would suggest a sequential search e.g. as below. This should search the common story ranges in a document, but if not the coding gets more complicated to cover the less common ones, to which end I would suggest Document Batch Processes,
See also Replace using wildcards


Code:
Option Explicit

Sub Macro1()
Dim vFindText As Variant
Dim i As Long
Dim oStory As Range
    vFindText = Array("25.222", "25.2666", "25.223")
   
    For Each oStory In ActiveDocument.StoryRanges
        For i = 0 To UBound(vFindText)
            With oStory.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .MatchWildcards = True
                Do While .Execute(findText:=vFindText(i))
                    oStory.HighlightColorIndex = wdTurquoise
                    oStory.Collapse 0
                Loop
            End With
        Next i
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For i = 0 To UBound(vFindText)
                    With oStory.Find
                        .ClearFormatting
                        .Replacement.ClearFormatting
                        .MatchWildcards = True
                        Do While .Execute(findText:=vFindText(i))
                            oStory.HighlightColorIndex = wdTurquoise
                            oStory.Collapse 0
                        Loop
                    End With
                Next i
            Wend
        End If
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-22-2023, 07:52 AM
jmdonhauser jmdonhauser is offline Macro to search word document for a several specific numbers and highlight them if they exist Windows 11 Macro to search word document for a several specific numbers and highlight them if they exist Office 2016
Novice
Macro to search word document for a several specific numbers and highlight them if they exist
 
Join Date: Sep 2023
Posts: 3
jmdonhauser is on a distinguished road
Default

Thank you so much! Your response is much appreciated. It worked for me. Seems like a such a basic thing to do (searching multiple numbers or words at once) that it would have been built into word.
Reply With Quote
  #4  
Old 09-22-2023, 10:21 AM
jmdonhauser jmdonhauser is offline Macro to search word document for a several specific numbers and highlight them if they exist Windows 11 Macro to search word document for a several specific numbers and highlight them if they exist Office 2016
Novice
Macro to search word document for a several specific numbers and highlight them if they exist
 
Join Date: Sep 2023
Posts: 3
jmdonhauser is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
It may not be possible to do this in a single pass, without knowing what else is in the document, so I would suggest a sequential search e.g. as below. This should search the common story ranges in a document, but if not the coding gets more complicated to cover the less common ones, to which end I would suggest Document Batch Processes,
See also Replace using wildcards


Code:
Option Explicit

Sub Macro1()
Dim vFindText As Variant
Dim i As Long
Dim oStory As Range
    vFindText = Array("25.222", "25.2666", "25.223")
   
    For Each oStory In ActiveDocument.StoryRanges
        For i = 0 To UBound(vFindText)
            With oStory.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .MatchWildcards = True
                Do While .Execute(findText:=vFindText(i))
                    oStory.HighlightColorIndex = wdTurquoise
                    oStory.Collapse 0
                Loop
            End With
        Next i
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For i = 0 To UBound(vFindText)
                    With oStory.Find
                        .ClearFormatting
                        .Replacement.ClearFormatting
                        .MatchWildcards = True
                        Do While .Execute(findText:=vFindText(i))
                            oStory.HighlightColorIndex = wdTurquoise
                            oStory.Collapse 0
                        Loop
                    End With
                Next i
            Wend
        End If
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Exit Sub
End Sub

Is it possible to modify this code (or use a separate macro) to verify if a second number in a document exists at the same time?

For example, using one of the numbers above (25.222).
I would like to check the document IF 25.222 exists, THEN does a second specific number (entered by the user) exists in the document simultaneously. So if 25.222 is in the document, I need to know 25.2666 is also in the document at the same time.
Maybe as an output, both would be highlighted or if one or the other doesn't exist, then a false statement or no highlighting.

Is that possible?

Thanks ahead!

J
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to search word document for a several specific numbers and highlight them if they exist VBA Highlight numbers only after or before specific words help Shelley Lou Word VBA 12 08-09-2022 03:02 AM
Macro to search word document for a several specific numbers and highlight them if they exist Highlight words in a table that does not exist in current document laith93 Word VBA 7 08-19-2021 03:51 AM
Macro to highlight and bolden specific text in Word Comments PCUSER Word VBA 1 09-22-2020 03:08 PM
Macro to search word document for a several specific numbers and highlight them if they exist Highlight numbers after a specific word in numbered list liblikas90 Word VBA 3 02-27-2019 03:52 AM
Macro to search word document for a several specific numbers and highlight them if they exist Macro to search for specific words in a document mike0215 Word VBA 2 11-28-2017 07:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:02 PM.


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