View Full Version : MS Word - Changing the shading table cells depending on the value
ChrisPine
01-28-2010, 09:17 PM
This post may have already been made, but i am trying to change the shading of a cell in a table in a word document. The shading will depend on the value of the cell.
For example: if the cell value is "Excellent", the cell shading is green
the cell values will come from a mail merge.
i have been looking around all day and i have received NO help from anything on Google. Maybe one of you guy's could help me out :)
Cheers
Regards,
Chris
macropod
01-29-2010, 02:04 AM
Hi Chris,
See: http://groups.google.com/group/microsoft.public.word.tables/browse_thread/thread/a4bb9c2509a7b68a/3148e2a2a510d65e?lnk=gst&q=form+-+field+shaded+when+blank%2C+unshaded+when+filled+in#3148e2a2a510d65e
RolfJ
01-29-2010, 07:15 AM
Hi Chris:
give this macro a try. Please note that I have been unable to get the cursor to land at the beginning of the ActiveDocument before the search starts. So you will have to do that by hand before your launch the macro.
Sub FindAndHighlight()
'First goto to the top of the first page
'THIS ISN'T WORKING. So please use CTRL + HOME
'before running this macro
Dim r As Range
Set r = ActiveDocument.GoTo(What:=wdGoToPage, which:=wdGoToAbsolute, Count:=1)
Set r = r.GoTo(What:=wdGoToLine, which:=wdGoToFirst)
Dim lastPosition As Integer
lastPosition = r.End
Do
Set r = ActiveDocument.GoTo(What:=wdTable, which:=wdGoToNext)
With r.Find
.Text = "Excellent"
'.Text = "NA"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.End <= lastPosition Then
Exit Do
Else
lastPosition = Selection.End
End If
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdNoHighlight
Options.DefaultHighlightColorIndex = wdBrightGreen
Selection.Range.HighlightColorIndex = wdBrightGreen
Loop
End Sub
fumei
01-29-2010, 10:33 AM
This can be done much simpler.
Option Explicit
Sub MakeGreen()
Dim oCell As Cell
Dim oTable As Table
Set oTable = ActiveDocument.Tables(1)
For Each oCell In oTable.Range.Cells
If InStr(1, oCell.Range.Text, "Excellent") > 0 Then
oCell.Range.HighlightColorIndex = wdBrightGreen
End If
Next
End Sub
RolfJ:
'First goto to the top of the first page
'THIS ISN'T WORKING. So please use CTRL + HOME
'before running this macro
1. Going to the start of the document is NOT needed.
2. you are using:
With r.Find
but then use:
Selection.Find.Execute
3. to move Selection to the start of a document (rarely, if ever needed), the VBA code is:
Selection.HomeKey Unit:=wdStory
Demo attached. Click "Make Green" on the top toolbar.
RolfJ
02-01-2010, 07:48 AM
Hi Fumei:
that's beautiful. Thanks for sharing!
fumei
02-01-2010, 09:54 AM
You are welcome.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.