How to shading of the cells into table?

A

avkokin

Hello.
There is the table which has some cells with text into these cells,
e.g. "abcd". I need to fill of color only these cells. I did so:
Sub tablecolor()
'
Dim rngTable As Range
Dim oTable As Table
Dim oRow As Row
Dim oCell As cell
Set rngTable = Selection.Range
Set oTable = Selection.Tables(1)
If Not rngTable.Information(wdWithInTable) Then
MsgBox "You are not within the table"
Else
With oTable
For Each oRow In .Rows
For Each oCell In oRow.Cells
If .Range.Text = "abcd" Then
oCell.Shading.BackgroundPatternColor = wdColorBlueGray
End If
Next oCell
Next oRow
End With
End If
End Sub

But it not work. Why? How to fill of color of necessary cells?
Thank you.
 
S

StevenM

To: Avkokin,

Two problem which are solved by changing the line:
If .Range.Text = "abcd" Then

to read:
If Left(oCell.Range.Text, 4) = "abcd" Then

Do you know how to debug your macros? If you would have stepped through your
macro line by line, you would have found out that it never went to the
following line.

oCell.Shading.BackgroundPatternColor = wdColorBlueGray

And if it never gets to that line, nothing happens.

..Text from a table cell always contains two extra characters at the end
which need to be stripped. There are a number of ways of getting rid of
them, but you must remember that they are there. Thus: oCell.Range.Text =
"abcd" will alway be false.

Another way of writing your code would have been:

Dim sStr as String
'
'
For Each oCell In oRow.Cells
sStr = oCell.Range.Text
sStr = Left(sStr, Len(sStr) - 2)
If sStr = "abcd" Then

The difference between this and what I wrote above is that this will color a
cell only if "abcd" is in the cell. Whereas:

If Left(oCell.Range.Text, 4) = "abcd" Then

will color the cell if the cell begins with "abcd" and thus "abcdefg" would
also be counted as true.

Steven Craig Miller
 
A

avkokin

To: Avkokin,

Two problem which are solved by changing the line:
If .Range.Text = "abcd" Then

to read:
If Left(oCell.Range.Text, 4) = "abcd" Then

Do you know how to debug your macros? If you would have stepped through your
macro line by line, you would have found out that it never went to the
following line.

oCell.Shading.BackgroundPatternColor = wdColorBlueGray

And if it never gets to that line, nothing happens.

.Text from a table cell always contains two extra characters at the end
which need to be stripped. There are a number of ways of getting rid of
them, but you must remember that they are there. Thus: oCell.Range.Text =
"abcd" will alway be false.

Another way of writing your code would have been:

Dim sStr as String
'
'
For Each oCell In oRow.Cells
sStr = oCell.Range.Text
sStr = Left(sStr, Len(sStr) - 2)
If sStr = "abcd" Then

The difference between this and what I wrote above is that this will color a
cell only if "abcd" is in the cell. Whereas:

If Left(oCell.Range.Text, 4) = "abcd" Then

will color the cell if the cell begins with "abcd" and thus "abcdefg" would
also be counted as true.

Steven Craig Miller

Thank's. I'm understand.
Sincerely, Anton Kokin
 

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