Assume you have a table in your Word document, and you wish to incorporate a color-coded drop-down list in one of the table columns. This means that when you choose an option from the drop-down, the cell color changes to red, and selecting another option turns the cell color green, as illustrated in the screenshot. How can you accomplish this in a Word document?
Inserting a color-coded drop-down list in a Word document involves the following steps, utilizing VBA code:
- Insert the Drop-Down List:
- Select a cell in the table where you want to insert the drop-down.
- Click on the “Developer” tab and choose the “Drop-Down List Content Control” icon.
- The drop-down will be added to the specified cell.
- Click on “Developer” again and select “Properties.”
- In the “Content Control Properties” dialog, perform the following:
- Enter a title into the “Title” text box.
- Click “Add” to access the “Add Choice” dialog.
- In the “Add Choice” dialog, type drop-down list items into the “Display Name” text box.
- Repeat step 3 to add other drop-down list items.
- After creating the first drop-down list, copy and paste it into other cells as needed.
- Apply VBA Code:
- Press
Alt + F11
to open the “Microsoft Visual Basic for Applications” window. - Double-click on “ThisDocument” in the Project Explorer to open the code window.
- Copy and paste the provided VBA code into the blank module.
- Press
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl.Range
If ContentControl.Title = “Status” Then
Select Case .Text
Case “Complete”
.Cells(1).Shading.BackgroundPatternColor = wdColorRed
Case “In Progress”
.Cells(1).Shading.BackgroundPatternColor = wdColorGreen
Case “Not Start”
.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case Else
.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End With
End Sub
- Customize the title (“Status”) and options (“Complete,” “In Progress,” “Not Start”) in the VBA code according to your requirements.
- Save and close the code window.
- Close and reopen the Word document to enable the macro.
Now, selecting an item from the drop-down list will dynamically change the cell color based on the chosen option.