I have a table that is 8 columns across and multiple rows. The 2nd row is my
conditional. If it is filled with "X" or "x" it will fomat the last 5
columns of that row in a dark gray background. If it is Blank, it will not
do anything. This needs to start on the first row and run until the end of
the document.
I have been able to create the macro that turns the last 5 columns gray
(With my cursor in the 2nd cell) . This will work for doing this one row at
a time, but I would prefer to do this all at once by clicking in row 1 / cell
2. I found something else with FOR/NEXT, but not able to get it to work.
This, in theory, is what I am looking for below.
[code]
Sub Macro3()
Dim TargetText As String
Dim oRow As Row
If Selection.Information(wdWithInTable) = False Then Exit Sub
TargetText = Chr(88) Or Chr(120)
For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(2).Range.Text = TargetText & vbCr & Chr(7) Then
Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorGray15
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorAutomatic
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Next oRow
End Sub
[/code]
Thank you.
Gene
Helmut Weber - 18 Mar 2008 16:55 GMT
Hi,
have a look at this one:
Sub Test4a()
Dim oRow As Row
Dim rTmp As Range
Dim sTmp As String
If Selection.Information(wdWithInTable) = False Then Exit Sub
Set rTmp = Selection.Range
' TargetText = Chr(88) Or Chr(120) ' type mismatch
For Each oRow In Selection.Tables(1).Rows
sTmp = Left(oRow.Cells(2).Range.Text, 1)
If sTmp = Chr(88) Or sTmp = Chr(120) Then ' watch out
rTmp.Start = oRow.Cells(5).Range.Start
rTmp.End = oRow.Cells(8).Range.End
rTmp.Shading.Texture = wdTextureNone
rTmp.Shading.ForegroundPatternColor = wdColorAutomatic
rTmp.Shading.BackgroundPatternColor = wdColorGray15
End If
Next oRow
End Sub
Note that this one doesn't work:
TargetText = Chr(88) Or Chr(120) ' type mismatch
Chr(88) Or Chr(120) returns a boolean
which can not be assigned to a string.
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Vista Small Business, Office XP