Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #29  
Old 02-22-2020, 10:23 PM
macropod's Avatar
macropod macropod is offline Macro to highlight alternating sections of a table Windows 7 64bit Macro to highlight alternating sections of a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
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

As coded, the macro requires the heading row parameter to be present, to make it optional, it needs to be the last parameter:
Parms: Col=3, RGB=215,199,244, Hdrs=0
The macro can then be recoded as:
Code:
Sub TblHiLite()
Application.ScreenUpdating = False
Dim Hdr As Long, S As Long, W As Long, C As Long, R As Long, H As Long, i As Long
Dim StrTitle As String, StrParms As String, StrRGB As String
'Abort if the cursor is not in a table
If Selection.Information(wdWithInTable) = False Then
  MsgBox "The selection is not in a table!", vbOKOnly, "TblHiLite"
  GoTo ErrExit
End If
'Get Parameters
On Error GoTo ErrExit
StrParms = Split(Selection.Tables(1).Range.Previous.Paragraphs.Last.Range.Text, vbCr)(0)
For i = 0 To UBound(Split(StrParms, "="))
  Select Case i
    i = 0: C = Trim(Split(Split(StrParms, "=")(1), ",")(0))
    i = 1: StrRGB = Trim(Split(Split(StrParms, "=")(2), ",")(0))
    i = 2: Hdr = Trim(Split(StrParms, "=")(3))
  End Select
Next
S = RGB(Split(StrRGB, ",")(0), Split(StrRGB, ",")(1), Split(StrRGB, ",")(2))
On Error GoTo 0
W = RGB(255, 255, 255)
'process the table
With Selection.Tables(1)
  'Validate the column count
  If C > .Columns.Count Then
    MsgBox "There is no column " & C & " in the table!", vbOKOnly, "TblHiLite"
    End
  End If
  'Validate the heading row count
  If Hdr = 0 Then
    For i = 1 To .Rows.Count
      If .Rows(i).HeadingFormat = True Then
        Hdr = Hdr + 1
      Else
        Exit For
      End If
    Next
  ElseIf Hdr > .Rows.Count Then
    MsgBox "There is no row " & Hdr & " in the table!", vbOKOnly, "TblHiLite"
    End
  End If
  StrTitle = Split(.Cell(1 + Hdr, C).Range.Text, vbCr)(0)
  .Rows(1 + Hdr).Shading.BackgroundPatternColor = W: H = W
  For R = 2 + Hdr To .Rows.Count
    If Split(.Cell(R, C).Range.Text, vbCr)(0) <> StrTitle Then
      If H = W Then H = S Else H = W
      StrTitle = Split(.Cell(R, C).Range.Text, vbCr)(0)
    End If
    .Rows(R).Shading.BackgroundPatternColor = H
  Next
End With
Application.ScreenUpdating = True
End
ErrExit:
MsgBox "Invalid Parameter", vbExclamation
End Sub
Now, if the 'Hdrs=#' parameter is missing or set to 0, the code will work it out for itself.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



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 06:04 AM.


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