Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2020, 12:00 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Macro to highlight alternating sections of a table Windows XP Macro to highlight alternating sections of a table Office 2007
Competent Performer
Macro to highlight alternating sections of a table
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default


I tweaked the code a bit. Here is a working macro. The target text must be in column 1 and the first row is assumed to be a header.

Code:
Sub TblHiLite()

Const MyName As String = "TblHiLite"
'Const Blue As Long = 15849926   'Light blue (198,217,241)
Const Blue As Long = 15853019   'Light blue (219,229,241)
Const White As Long = 16777215  'No highlighting

'Abort if the cursor is not in a table
If Selection.Information(wdWithInTable) <> True Then
  MsgBox "The cursor is not in a table", vbOKOnly, MyName
  Exit Sub
End If

Application.ScreenUpdating = False
Dim Row As Long           'Row number (loop index)
Dim HiLiteColor As Long   'The ?????
Dim TgtTextNew As String  'The target text string
Dim TgtTextOld As String  'The previous target text string

HiLiteColor = Blue                'Initialize color
With Selection.Tables(1)          'Focus the selection on the table
  For Row = 2 To .Rows.Count         'Loop through all row but the header
    TgtTextNew = Split(.Cell(Row, 1).Range.Text, vbCr)(0) 'Get next text string
    If TgtTextNew <> TgtTextOld Then  'If it's a new section
      TgtTextOld = TgtTextNew           'Save the text
                                        'Switch colors
      If HiLiteColor = White Then HiLiteColor = Blue Else HiLiteColor = White
    End If
    .Rows(Row).Shading.BackgroundPatternColor = HiLiteColor 'Apply highlighting
  Next Row
End With
  
Application.ScreenUpdating = True

End Sub
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to highlight alternating sections of a table Macro to highlight a list of words bakerkr Word VBA 4 10-19-2017 02:23 PM
Macro to highlight alternating sections of a table Word 2010 VBA Print Macro - Specified Sections Benbon Word VBA 3 03-30-2017 02:31 PM
Macro to highlight alternating sections of a table Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM
Macro to highlight alternating sections of a table Macro to highlight words bertietheblue Word VBA 9 07-01-2013 12:39 PM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:46 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