Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-14-2015, 08:19 PM
kristib kristib is offline Macro to color row of Word table based on found text Windows 8 Macro to color row of Word table based on found text Office 2013
Novice
Macro to color row of Word table based on found text
 
Join Date: Nov 2015
Posts: 3
kristib is on a distinguished road
Lightbulb Macro to color row of Word table based on found text

I need a macro to find a text string "{Friday Workshop}" in a table. If that string is found, I want the entire row of the table it is in to be filled with a color (let's say yellow). There will be several tables in the document, and several instances of the string should be found.

There will be other text strings, too, like "{Saturday Breakfast}" or "{Sunday Workshop}". All rows that contain any of these strings need to be highlighted yellow. The text will always be in the far left column of the table, if that matters.

(Note I have the brackets as part of my strings so I can delete that text easily after highlighting all the appropriate rows.)



Thank you!
Reply With Quote
  #2  
Old 11-14-2015, 10:01 PM
macropod's Avatar
macropod macropod is offline Macro to color row of Word table based on found text Windows 7 64bit Macro to color row of Word table based on found text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\{[MTWFS][ondayueshrit]{2,7} Workshop\}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) = True Then
      .Rows(1).Shading.BackgroundPatternColorIndex = wdYellow
      '.Delete
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
If, as your post suggests, you want to delete the found text, simply un-comment '.Delete'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-15-2015, 02:17 PM
kristib kristib is offline Macro to color row of Word table based on found text Windows 8 Macro to color row of Word table based on found text Office 2013
Novice
Macro to color row of Word table based on found text
 
Join Date: Nov 2015
Posts: 3
kristib is on a distinguished road
Default

Thank you so much for replying. I used your exact code, except I replaced the Find text with my actual text. I am getting a Run-time error 5560: The Find What text contains a pattern match expression which is not valid.

My code below:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "{Sat Breakfast}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
.Rows(1).Shading.BackgroundPatternColorIndex = wdYellow
'.Delete
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub

Any other ideas?
Reply With Quote
  #4  
Old 11-15-2015, 02:33 PM
macropod's Avatar
macropod macropod is offline Macro to color row of Word table based on found text Windows 7 64bit Macro to color row of Word table based on found text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Your code fails because it lacks the \ before the { and }. Without that, those characters act as wildcard escape characters ebut what you have between them then doesn't conform to what a wildcard search requires to be between them.

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\{[MTWFS][onuedhuriat]{2} Workshop\}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) = True Then
      .Rows(1).Shading.BackgroundPatternColorIndex = wdYellow
      '.Delete
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-15-2015, 02:42 PM
kristib kristib is offline Macro to color row of Word table based on found text Windows 8 Macro to color row of Word table based on found text Office 2013
Novice
Macro to color row of Word table based on found text
 
Join Date: Nov 2015
Posts: 3
kristib is on a distinguished road
Default

Works beautifully! And the Delete part, too. Thank you very much!!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to color row of Word table based on found text Inserting a Merged Row after a certain text is found in a table Snaybot Word VBA 13 09-30-2015 03:12 PM
Macro to color row of Word table based on found text Table, font color based on what word is in cell donaldadams1951 Word Tables 1 04-15-2015 08:37 PM
Color-fill a range of cells, based on text in a different sheet. Possible? unittwentyfive Excel 2 06-01-2014 06:48 AM
Macro to color row of Word table based on found text Word VBA Macro to Find and Replace based on the Alt Text of an Image bennymc Word VBA 1 01-27-2014 04:23 PM
Macro to color row of Word table based on found text Word 2003 - Macro to color a row in table? cyberpaper Word 2 01-03-2013 02:07 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:15 AM.


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