Alternate grey shading for selected rows of a table

A

andreas

Dear Experts:

with x rows of a table selected I would like to activate a macro
that ...

.... alternately shades the selected rows grey. The grey shading should
begin on the uppermost selected row.

I GOT a macro that does this alternate shading much more comfortable
(see macro below <TblAltShadingGrey>), but as soon as the macro hits
vertically merged cells, it throws an error message. Therefore I would
like to be able to apply alternate grey shading to selected rows.

I am also aware that a user-defined table style could do the above job
(alternate grey shading) very easily. But there are a couple of Word
2000 documents where I have to perform these tasks.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub TblAltShadingGrey()

Dim oRow As row
Dim rng As Word.range
Dim tbl As Table
Dim StartRow As Long
Dim ShadedRow As Boolean


If Not Selection.Information(wdWithInTable) Then
MsgBox "Please place the cursor into the table", vbOKOnly +
vbCritical, "Alternate Grey Shading for selected Table"
Exit Sub
End If


StartRow = InputBox("Grey Shading: Enter starting row:", "Alternate
Grey Shading")

Set tbl = Selection.Tables(1)
Set rng = tbl.range

For Each oRow In tbl.rows
If oRow.Index >= StartRow Then
ShadedRow = Not ShadedRow
If ShadedRow Then
oRow.Cells.Shading.BackgroundPatternColor = RGB(225, 225,
225)
Else
oRow.Cells.Shading.BackgroundPatternColor =
wdColorAutomatic
End If
End If
Next

End Sub
 
D

Doug Robbins - Word MVP

Once you have merged cells in a table, about 99.9% of the things that you
can do to it with VBA are crippled.

You cannot for instance use VBA to split the table. Otherwise, you could
have used VBA to split the table above and below the selected rows, then
change the shading to the table that contained the selected rows and then
remove the splits.

Your only option therefore is to manually split the table, run the macro to
apply the shading and then manually remove the splits.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
G

Graham Mayor

As Doug indicates you are limited in what you can do with tables and you
cannot process rows with vba, where the rows are vertically merged, but you
could process the cells. You cannot however shade part of a range of
vertically merged cells. The following will shade the second and alternate
rows. Whether the merged cells will shade or not rather depends on whether
the uppermost part of the merged cell is in a shaded 'row' or not. If you
want to shade the first and alternate rows change the line
If aCell.RowIndex Mod 2 = 0 Then
to
If aCell.RowIndex Mod 2 <> 0 Then
Horizontally merged cells will shade or not according to the row.

Dim oTable As Table
Dim aCell As Cell
Set oTable = Selection.Tables(1)
With oTable
For Each aCell In oTable.Range.Cells
If aCell.RowIndex Mod 2 = 0 Then
aCell.Shading.BackgroundPatternColor = _
RGB(225, 225, 225)
End If
Next aCell
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

If you want to choose the whether to start alternate shading from the first
row or second row -

Dim oTable As Table
Dim aCell As Cell
Dim sAsk As String
Dim iFirst As Integer
sAsk = MsgBox("Shade the first row?", vbYesNo, _
"Table shading")
If sAsk = vbYes Then iFirst = 1 Else iFirst = 0
Set oTable = Selection.Tables(1)
With oTable
For Each aCell In oTable.Range.Cells
If aCell.RowIndex Mod 2 = iFirst Then
aCell.Shading.BackgroundPatternColor = _
RGB(225, 225, 225)
Else
aCell.Shading.BackgroundPatternColor = _
wdColorAutomatic
End If
Next aCell
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

andreas

Once you have merged cells in a table, about 99.9% of the things that you
can do to it with VBA are crippled.

You cannot for instance use VBA to split the table.  Otherwise, you could
have used VBA to split the table above and below the selected rows, then
change the shading to the table that contained the selected rows and then
remove the splits.

Your only option therefore is to manually split the table, run the macro to
apply the shading and then manually remove the splits.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my serviceson
a paid professional basis.



















- Show quoted text -

Dear Doug,

thank you very much for your in-depth explanation of tables featuring
vertically merged cells

Regards, Andreas
 
A

andreas

If you want to choose the whether to start alternate shading from the first
row or second row -

Dim oTable As Table
Dim aCell As Cell
Dim sAsk As String
Dim iFirst As Integer
sAsk = MsgBox("Shade the first row?", vbYesNo, _
    "Table shading")
If sAsk = vbYes Then iFirst = 1 Else iFirst = 0
Set oTable = Selection.Tables(1)
With oTable
    For Each aCell In oTable.Range.Cells
        If aCell.RowIndex Mod 2 = iFirst Then
            aCell.Shading.BackgroundPatternColor = _
            RGB(225, 225, 225)
        Else
            aCell.Shading.BackgroundPatternColor = _
            wdColorAutomatic
        End If
    Next aCell
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>








- Show quoted text -


Hey Graham,

great job! Both codes are of use to me. Thank you very much for your
professional help.

Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top