Conditional formatting in a table based on cell content with a mac

G

Gene-Salkovsky

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
Thank you.
Gene
 
H

Helmut Weber

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
 

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