A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Shading cells based on content



 
 
Thread Tools Display Modes
  #1  
Old December 15th, 2004, 08:58 PM
AOH
external usenet poster
 
Posts: n/a
Default Shading cells based on content

Hi, I'm looking for a way to automatically add shading to a row of
cells. What I have is a two-column table, and I'd like to shade the
rows which contain a particular word in the first cell. I can't find a
way to do this using table styles, so I think a macro is the way
forward. My VB is pretty much nonexistent but I'm willing to
learn...can anyone point me in the right direction to get me started?
Using Word 2003, Win XP. Thanks

  #2  
Old December 15th, 2004, 10:07 PM
Greg Maxey
external usenet poster
 
Posts: n/a
Default

AOH,

I am a VBA beginner myself and so the code I am providing can probably be
improved. Still it seems to work.
Sub ScratchMacro()
'Looks for the text string "????" in the first column and shades
'the corresponding row. Replace "????" in the InStr expression below with
the
'word or text string you want to search for
Dim oCell As Cell
Dim oRow As Row
Dim oCol As Column
Dim oTable As Table
Dim i As Long
If Selection.Information(wdWithInTable) = True Then
Set oTable = Selection.Tables(1)
Set oCol = Selection.Columns(1)
i = 0
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In oCol.Cells
i = i + 1
If InStr(oCell.Range.Text, "????") = 1 Then
Set oRow = oTable.Rows(i)
oRow.Shading.BackgroundPatternColorIndex = wdGray25
Set oRow = Nothing
End If
Next
End Sub



--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

AOH wrote:
Hi, I'm looking for a way to automatically add shading to a row of
cells. What I have is a two-column table, and I'd like to shade the
rows which contain a particular word in the first cell. I can't find a
way to do this using table styles, so I think a macro is the way
forward. My VB is pretty much nonexistent but I'm willing to
learn...can anyone point me in the right direction to get me started?
Using Word 2003, Win XP. Thanks



  #3  
Old December 16th, 2004, 10:44 AM
AOH
external usenet poster
 
Posts: n/a
Default

Greg, that's fantastic - works exactly as I wanted. Thanks a lot!

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I change a cell value based on another cells color? JeremyVIS General Discussion 2 September 8th, 2004 04:16 AM
Gray Shading in Cells is cutting off TEXT Jbabin General Discussion 1 May 29th, 2004 04:57 PM
Counting based on date ranges and other values in adjacent cells Frank Kabel Worksheet Functions 1 February 19th, 2004 05:21 PM
Format cells based on other cell's value Bob Phillips Worksheet Functions 0 January 9th, 2004 12:41 PM
Formula to color cells based on result David McRitchie Worksheet Functions 1 January 5th, 2004 04:14 PM


All times are GMT +1. The time now is 11:26 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.