Share via

how to create macro to shade a certain cell value?

Anonymous
2023-08-25T16:40:09+00:00

I have a Word table and want to keep it a Word table, but need a conditional macro that will:

- Lets say shade the cell yellow if the value is "Yes", Shade the cell red if the value is "no"

and if possible explain how to insert this macro to populate the table to act upon the inserted macro

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Charles Kenyon 167.5K Reputation points Volunteer Moderator
    2023-08-25T18:36:45+00:00

    Conditional formatting in Word is much more complex than in Excel. You would be able to do this far easily if your table were an Excel part in your Word document. You've said you want to maintain it as a Word table. I assume you mean you do not even want an Excel part in the document.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-08-25T18:39:07+00:00

    Hello Geraldine C! My name is Jeremy and I’m happy to help you with your question.

    I generated this macro below to help give you an idea on how to set up the VBA to color a cell based on a yes or no question, which in your case would be, is it of this certain value, yes, then shade.

    Sub ShadeCellsBasedOnValue() Dim tbl As Table Dim cell As Cell

    ' Specify the table you want to work with Set tbl = ActiveDocument.Tables(1) ' Change the index to match your table's index

    ' Loop through each cell in the table For Each cell In tbl. Range.Cells If cell. Range.Text = "Yes" Then cell. Shading.BackgroundPatternColor = wdColorYellow ElseIf cell. Range.Text = "No" Then cell. Shading.BackgroundPatternColor = wdColorRed End If Next cell End Sub

    As a precautionary note, it is always smart to back up your data before making changes.

    Regards,

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments