跳到主要内容

如何在Word表中插入颜色编码的下拉列表?

假设我的Word文档中有一个表,现在,我想在表的列中插入颜色编码的下拉列表。 这意味着当我从下拉列表中选择一个选项时,单元格颜色变为红色,而当我在下拉列表中选择另一选项时,单元格颜色变为绿色,如以下屏幕截图所示。 您如何在Word文档中解决这项工作?

doc颜色编码的下拉列表

使用VBA代码在Word文档中插入颜色编码的下拉列表


使用VBA代码在Word文档中插入颜色编码的下拉列表

以下步骤可以帮助您根据需要完成此任务,首先,插入下拉列表,然后为下拉列表应用颜色。 请这样做:

1。 在表格中选择要插入下拉列表的单元格,然后单击 开发商 > 下拉列表内容控制 图标,请参见屏幕截图:

doc颜色编码下拉菜单1

2。 下拉列表插入到特定的单元格,然后单击 开发商 > 查看房源,请参见屏幕截图:

doc颜色编码下拉菜单2

3。 在 内容控制属性 对话框,请执行以下操作:

(1.)将标题名称输入到 标题 文本框;

(2.)点击 地址 按钮转到 添加选择 对话;

(3.)在 添加选择 对话框中,将下拉列表项输入到 显示名称 文本框。

doc颜色编码下拉菜单3

4. 重复步骤3,根据需要插入其他下拉列表项。

5。 创建第一个下拉列表后,您可以根据需要将其复制并粘贴到其他单元格中。 看截图:

doc颜色编码下拉菜单4

6。 然后,您应该应用VBA代码,请按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

7。 在 Microsoft Visual Basic应用程序 窗口,双击 这个文件 来自 工程项目 窗格以打开模式,然后将以下代码复制并粘贴到空白模块中。

VBA代码:将颜色编码的下拉列表插入Word文档的表中:

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

doc颜色编码下拉菜单5

备注:在上面的代码中, Status 是创建下拉列表时的标题名称,并且 完成:, 处理中, 不开始 是下拉列表中的项目,您可以将其更改为自己的项目。 您还可以根据需要更改颜色。

8。 然后保存并关闭代码窗口,现在,当您从下拉列表中选择一项时,其相对颜色将被单元格填充,请参见屏幕截图:

doc颜色编码的下拉列表


 

最佳办公生产力工具

Kutools for Word - 通过 Over 提升您的文字体验 100 显着特点!

深入了解下面突出显示的功能或 单击此处探索 Kutools for Word 的全部功能.

📘 文档掌握: 分页  /  合并文件  /  以各种格式导出选择(PDF/TXT/DOC/XLSX)  /  批量转换为PDF  /  将页面导出为图像  /  一次打印多个文件 ...

内容编辑: 批量查找和替换 跨多个文件   /  调整所有图片的大小   /  转置表行和列  /  将表格转换为文字 ...

🧹 轻松清洁扫开 多余的空间  /  分节符  /   所有标题  /  文本框  /  超链接  /  如需更多删除工具,请前往我们的删除组

创意插入: 插页 千位分隔符  /  复选框  /  单选按钮  /  二维码  /  条码  /  对角线表  /  公式标题  /  图片说明  /  表标题  /  多张图片  /  在我们的插入组中发现更多信息

🔍 精准选择: 精确定位特定页面  /    /  形状  /  标题段落  /  使用我们的选择组轻松导航

星级增强: 快速导航至任何位置  /  自动插入重复文本  /  在文档窗口之间无缝切换  /  11 转换工具 ...

使用 Kutools 转换您的 Word 任务。 👉 立即下载并试用 30 天 🚀。

了解更多       免费下载      购买
 

 

Comments (51)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
I have a word document that i would like some help with, in short, i have a large inspection table, items either comply, don't, further information etc. I currently use a simple drop-down option which has 5-6 options, ill like a selection option that when different options are selected, that changes the cells/row to a red or green etc, and then also (If possible) updates a Summary Table? is this possible, I'll pay someone for a general templet i can the work on....

Thank you in advance....
This comment was minimized by the moderator on the site
Hello, peter,
Did you mean to select an item from the drop down list, and a specific background color is filled for that row?
Please view the attachment to check if it is your need. If this is not your need, please give your problem more clearly.
Thank you!
This comment was minimized by the moderator on the site
Almost, by changing the drop-down tab the whole row changes color, perfect, thank you, however the magic is then coping the row (Yet only rows that don't comply or need further info etc... ) and placing it in a summary table at the top of the page in another table etc... getting them all to keep adding would be great in a summary table.

I can then alter the summary table as things change, and it updated the main parent table .... I'm not sure this is possible.

I'm happy to have a Teams Meeting to get this working, it would be apricated as it is doing my head in...

This comment was minimized by the moderator on the site
Hi, this is great, got me over a major problem with my fillable form. However, when I go to protect my fillable form, it brings up the debugger box? any idea how i can overcome this?
This comment was minimized by the moderator on the site
Thank you, i have managed to change the background colour. Is there a way i can change the text to white?
This comment was minimized by the moderator on the site
Hello, Emily,
To change the text color to white, please apply the following code:
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
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "In Progress"
                .Cells(1).Shading.BackgroundPatternColor = wdColorGreen
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "Not Start"
                .Cells(1).Shading.BackgroundPatternColor = wdColorBlue
                .Cells(1).Range.Font.Color = wdColorWhite
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
What code do you add so it goes back to the normal colours (both background and text) when you select "Choose an item" from dropdown list after previously selecting a case
This comment was minimized by the moderator on the site
Hello, Usama,
In this case, the code in this article can help you. When uou select an item and then do back to choosing "Choose an item", the backdround color will not be filled.
Please try again. Thank you!
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Hi I want a drop down list of colours but i do not want any text in the cells, i just want my users to be able to choose a colour as I am creating a RAG rated form.

is this possible?

thanks.
This comment was minimized by the moderator on the site
I have pasted the code and can see my drop down options (the text) but when I choose them, the background colors don't apply. Do you have any way to help me figure out what I may be doing wrong? Please and thank you! This is exactly what I'm hoping to do in my document, if I can make it work. Thanks!
This comment was minimized by the moderator on the site
Hello, Susan
You should check if the corresponding text in the VBA code has been mofified to your own contents.

Note: In the above code, Status is the title name when you creating the drop down list, and Complete, In Progress, Not Start are the items of the drop down list, you can change them to your own. And you can also change the color to your need.

If there is still problem after changing the text, you can upload the attachment word file and we will help you check it.
Thank you!
This comment was minimized by the moderator on the site
I am having the same issue.
I have changed the titles names in the code and followed the rest of the instructions.
Can you please tell me what I may have done wrong?
This comment was minimized by the moderator on the site
Hi, is there any way to color code-vba the text in a dropdown if it is not in a table or cell? Thanks!
This comment was minimized by the moderator on the site
Hello, nikki
If you just want to color coded the values from the drop down list, maybe the following code can help you:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
  If .Title = "Status" Then
    Select Case .Range.Text
      Case "Complete": .Range.Font.ColorIndex = wdRed
      Case "In Progress": .Range.Font.ColorIndex = wdBlue
      Case "Not Start": .Range.Font.ColorIndex = wdGreen
      Case Else: .Range.Font.ColorIndex = wdAuto
    End Select
  End If
End With
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Hi, is there way to change the color of the text in the dropdown if it is not in a table or cell?

Thanks!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations