View Single Post
 
Old 02-18-2020, 08:48 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Windows XP Office 2007
Competent Performer
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Macro to highlight alternating sections of a table

I have a table of results for a collection of experiments. Each experiment has a title and may be run multiple times with different criteria. When I sort the table by the title, I like to highlight every other set of results with a light blue background. This helps be focus on the results for each experiment.

I did this manually once. But now I have added some new experiments that sort between existing experiments. Unless I redo the entire list, I will have two consecutive sets with the same highlighting (blue or none).

I would like to write a macro to redo the highlighting every time I add a new experiment, but my skills with Word VBA is not good enough.

Can someone give me the initial outline of a macro that will loop through each line (row) of a table, examine the title field, and change the highlighting whenever the title changes?

Here's a rough outline of what I think the macro needs to do. The code in parens is what I don't know how to do.

Code:
Sub TableHiLite()  'Not sure if it needs parameters

Dim HiLite as boolean
HiLite = True
Dim Row as Integer
Dim NumRows as Integer
 NumRows = (number of rows in the table)
Dim TitleNext as String
Dim TitlePrev as String

For Row = 2 to NumRows
  TitleNext = (title text in this row)
  If TitleNext <> TitlePrev then HiLite = Not HiLite
  If HiLite then (set the highlight for this row to light blue)
  Else (set the highlight for this row to light none)
Next Row

End Sub
Thanks

PS: Ooops. I just realized that this should have gone into the VBA section. Maybe someone can move it. Sorry.
Reply With Quote