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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
Greg, that's fantastic - works exactly as I wanted. Thanks a lot!
|
Thread Tools | |
Display Modes | |
|
|
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 |