MobileMobile | Continue

Excel Bar

Excel Bar

Your excel questions will be responsed by our excel experts within 24hrs.Our service is free.

 Forgot Pass?
 Register Now
Find
Hot Search: Vlookup Match VBA
View: 313|Reply: 7

Test for four digit number on page then colour word red

[Copy link]

5

Topics

15

Posts

45

Integral

Member

Rank: 2

Integral
45
Post on 9-14-2016 18:33:25 | All posts |Read mode
Hello all
This is my first foray into word macros, what I am hoping to achieve is if the four digit number, in this case 1375 appears on a page and the word Test appears on that page it will colour it red.
I have come up with a macro that works to the degree that if 1375 appears it will colour the word Test red but through the whole document, it maybe up to 80 pages at times.
How can I make changes so that it only changes the word Test to red on the pages that 1375 appear and not through the entire document.
this is the macro that I have at the moment
Sub Test()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
        .ClearFormatting
        .Text = 1375
End With

If Selection.Find.Execute Then
Selection.Select 'Selects the string if found, other Actions would go here.
    'Colour word red
    Dim arrWords, i As Long
arrWords = Array(Test)
With ActiveDocument.Content.Find
  .ClearFormatting
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = True
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  With .Replacement
    .ClearFormatting
    .Text = ^
    .Font.Color = wdColorRed
    .Font.Bold = False
  End With
  For i = 0 To UBound(arrWords)
    .Text = arrWords(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End If

End SubThanks in advance for any help




Reply

Props Report

2

Topics

188

Posts

466

Integral

Conqueror

Rank: 3Rank: 3

Integral
466
Post on 9-14-2016 19:01:37 | All posts



What relationship is there, if any, between where on the page '1375' and 'Test' appear?











Reply Support Opposition

Props Report

5

Topics

15

Posts

45

Integral

Member

Rank: 2

Integral
45
 Author| Post on 9-14-2016 20:20:49 | All posts



There is no relationship to the 2, just if they appear on the same page, usually the 1375 will be near the top left of the page and test could be anywhere on the page depending on how much info is included on that page.
So it does not have a usual location on the page.
Thanks for you reply.




Reply Support Opposition

Props Report

2

Topics

188

Posts

466

Integral

Conqueror

Rank: 3Rank: 3

Integral
466
Post on 9-14-2016 21:21:47 | All posts



In that case, try:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .Text = 1375
    .Wrap = wdFindStop
    .MatchWholeWord = True
    .Execute
  End With
  Do While .Find.Found
    Set Rng = .Duplicate.GoTo(What:=wdGoToBookmark, Name:=\page)
    With Rng.Find
      .Text = Test
      .Replacement.Text = ^
      .Replacement.Font.ColorIndex = wdRed
      .Wrap = wdFindStop
      .MatchCase = True
      .MatchWholeWord = True
      .Execute Replace:=wdReplaceAll
    End With
    .Start = Rng.End
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub









Reply Support Opposition

Props Report

5

Topics

15

Posts

45

Integral

Member

Rank: 2

Integral
45
 Author| Post on 9-14-2016 22:09:05 | All posts



Thank macropod your solution works great, much appreciate your time, cheers
Reply Support Opposition

Props Report

39

Topics

2573

Posts

5415

Integral

Forum Legend

Rank: 8Rank: 8

Integral
5415
Post on 1-8-2017 12:23:46 | All posts



Welcome to the forum! Do you have a question?











Reply Support Opposition

Props Report

1

Topics

6

Posts

15

Integral

Member

Rank: 2

Integral
15
Post on 1-8-2017 14:03:23 | All posts



Yes I have a question, trying to post but it fails so trying just with test




Reply Support Opposition

Props Report

39

Topics

2573

Posts

5415

Integral

Forum Legend

Rank: 8Rank: 8

Integral
5415
Post on 1-8-2017 15:37:06 | All posts



Are you trying to post a formula or some sort of code? If you are trying to post  or  , you need a space either side. For codes, use the # button to surround the text with code tags. Try opening a new thread, and then I'll get rid of this one for you.
Reply Support Opposition

Props Report

Points policy of this forum

Archiver|Mobile|Small dark house|Contact us|Excel Bar

GMT-5, 9-24-2017 21:23 , Processed in 0.251395 second(s), 20 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

!fastreply! Top !return_list!