PDA

View Full Version : Automate Conditional Formatting in Word



goldfinch123
10-23-2020, 04:55 AM
Hi, I have a table in word and want to automate red, amber or green shading in column 8 for each row. This starts in row 5 of the table as the 4 rows above are merged for titles. I know I can do this in Excel but I want to use Word for this table. Can anyone help? Thank you.

gmaxey
10-23-2020, 07:06 AM
As this is your first post I'm going to give you a fish. However, the purpose of this forum is to "assist" you write your own code. Not write it for you. Working with tables that have merged cells can be very difficult. By putting the cursor in the first cells (i.e., row five, column 8) this may work:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long: lngIndex = 1
Do
If lngIndex = 4 Then lngIndex = 1
Select Case lngIndex
Case 1
Selection.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case 2
Selection.Cells(1).Shading.BackgroundPatternColor = wdColorBrightGreen
Case 3
Selection.Cells(1).Shading.BackgroundPatternColor = wdColorLightYellow
End Select
lngIndex = lngIndex + 1
'Escape clause:
If Selection.Cells(1).Range.Rows(1).Range.End = Selection.Tables(1).Rows(Selection.Tables(1).Rows.Count).Range.End Then Exit Do
'Next row.
Selection.MoveRight Unit:=wdCell, Count:=Selection.Tables(1).Columns.Count
Loop
lbl_Exit:
Exit Sub
Err_Loop:
Resume lbl_Exit
End Sub

goldfinch123
10-23-2020, 07:21 AM
Hi Greg, that's very kind of you. I apologise, I didn't realise this was a site for people who knew code. That doesn't really apply to me. I will try a different site. I couldn't get the code to work. Kind regards.

gmaxey
10-23-2020, 07:26 AM
I'm not trying to run you off. I'm trying to tell you this isn't a "free" code writing service. We are here to help you find your solutions. That you should try to learn to do what you need to do with Word, just as you have with Excel.

"I couldn't get the code to work." isn't very helpful.

Can you attach a sanitized version of your document?

goldfinch123
10-23-2020, 07:51 AM
I'm not trying to run you off. I'm trying to tell you this isn't a "free" code writing service. We are here to help you find your solutions. That you should try to learn to do what you need to do with Word, just as you have with Excel.

"I couldn't get the code to work." isn't very helpful.

Can you attach a sanitized version of your document?

Many thanks, Greg. I put that I couldn't get the code to work because I didn't want to be a fraud on the site and not be working to its terms of reference! I am a complete numpty with code in Word, never done it before. I get basic stuff in Excel and not too shabby but never attempted outside of this application or Access, and I am at a basic level!! Doing a course at work to improve but early days.
Many thanks, I attach a sanitised version. Now column is 7 and not 8 and I apologise if that's not helped.

gmaxey
10-23-2020, 08:15 AM
Your table is more complex than you originally described. Try:

Sub Test()
Dim oCell As Cell
Dim lngIndex As Long
lngIndex = 1
For Each oCell In ActiveDocument.Tables(1).Range.Cells
If oCell.Range.Information(wdEndOfRangeColumnNumber) = 7 Then
If lngIndex = 4 Then lngIndex = 1
Select Case lngIndex
Case 1: oCell.Shading.BackgroundPatternColor = wdColorBlue
Case 2: oCell.Shading.BackgroundPatternColor = wdColorBrightGreen
Case 3: oCell.Shading.BackgroundPatternColor = wdColorLightYellow
End Select
lngIndex = lngIndex + 1
End If
Next
End Sub

goldfinch123
10-27-2020, 07:02 AM
Hi Greg, very many thanks for your reply with code. I will give this a go. Apologies for late reply, not been well. Kind regards.