Macro for Striped Table

D

debbieg

Hi,
I hope I have this posted in the right group...so many choices.
I tried making my first macro tonight.
I'm using the code below in a macro to stripe a table.
As is, it makes every row white.
I need to make two rows white and the next two rows green and repeat this to
the end of the table.
Thanks a bunch,
Debbie

Dim oTbl As Table
Dim nRow As Long

If Selection.Information(wdWithInTable) Then
Set oTbl = Selection.Tables(1)
For nRow = 1 To oTbl.Rows.Count Step 1
oTbl.Rows(nRow).Shading.BackgroundPatternColor _
= wdColorWhite
Next
End If
 
A

alborg

Hi deb:

To make it work, you have to set up an array, and for this example, we'll
declare a variable called "lngTicker" to step through the color schemes:

Dim lngTicker As Long

If Selection.Information(wdWithInTable) Then
Set oTbl = Selection.Tables(1)
For nRow = 1 To oTbl.Rows.Count Step 1

lngTicker = 1
Select Case lngTicker
Case 1
lngTicker = 2
If lngTicker = 1 and
oTbl.Rows(nRow).Shading.BackgroundPatternColor _
= wdColorWhite
Case 2
lngTicker = 3
oTbl.Rows(nRow).Shading.BackgroundPatternColor _
= wdColorWhite
Case 3
lngTicker = 4
If lngTicker = 1 and
oTbl.Rows(nRow).Shading.BackgroundPatternColor _
= wdColorGreen
Case 4
lngTicker = 1
oTbl.Rows(nRow).Shading.BackgroundPatternColor _
= wdColorGreen
Next
End Select
End If

I'll let you test drive it! Let me know if there are any bugs...

Cheers,
Al
 
D

Doug Robbins - Word MVP

Use the following:

Dim i As Long, j As Long
With Selection.Tables(1)
For i = 1 To .Rows.Count Step 2
If i Mod 4 = 1 Then
For j = 0 To 1
.Rows(i + j).Shading.BackgroundPatternColor =
wdColorLightGreen
Next j
End If
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

alborg

Oh- I just realized that the code is screwed up. My keyboard must have
jammed. <g> It should look like this:

Dim lngTicker As Long, nRow as Long

If Selection.Information(wdWithInTable) Then
Set oTbl = Selection.Tables(1)
lngTicker = 1

For nRow = 1 To oTbl.Rows.Count Step 1
Select Case lngTicker
Case 1
lngTicker = 2
oTbl.Rows(nRow).Shading.BackgroundPatternColor = wdColorWhite
Case 2
lngTicker = 3
oTbl.Rows(nRow).Shading.BackgroundPatternColor = wdColorWhite
Case 3
lngTicker = 4
oTbl.Rows(nRow).Shading.BackgroundPatternColor = wdColorGreen
Case 4
lngTicker = 1
oTbl.Rows(nRow).Shading.BackgroundPatternColor = wdColorGreen
End Select
Next

End If

Cheers,
Al
 

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