跳至主要内容

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

Author: Xiaoyang Last Modified: 2025-05-08

假设您的Word文档中有一个表格,现在您希望在表格的某一列中插入一个颜色编码的下拉列表。这意味着当您从下拉列表中选择一个选项时,单元格的颜色会变成红色;而当您选择另一个选项时,单元格的颜色会变成绿色,如下图所示:

Demo showing a cell's color changing when an item is selected from the drop-down list

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

以下步骤将指导您完成在表格单元格中插入下拉列表,并根据所选项目应用单元格背景颜色的操作:

  1. 在表格中选择要插入下拉列表的单元格,然后点击“开发工具”>“下拉列表内容控件”。

    注意:如果在功能区中看不到“开发工具”选项卡,请访问:如何在Word功能区中显示“开发工具”选项卡

    Drop-Down List Content Control button on the ribbon
  2. 下拉列表被插入到指定的单元格中。然后点击“开发工具”>“属性”。
  3. Drop-down inserted into a specific cell with Properties option highlighted
  4. 在“内容控件属性”对话框中,执行以下操作:
    1. 在“标题”文本框中输入标题名称。
    2. 点击“添加”按钮以打开“添加选项”对话框。
    3. 在“显示名称”文本框中输入下拉列表项。
      Content Control Properties dialog box and Add Choice dialog box
  5. 重复步骤3以根据需要插入其他下拉列表项。
  6. 创建第一个下拉列表后,您可以将其复制并粘贴到其他单元格中。
    Drop-down list copied to other cells
  7. 按住ALT + F11打开“Microsoft Visual Basic for Applications”窗口。
  8. 在Microsoft Visual Basic for Applications窗口中,双击“Project”窗格中的“ThisDocument”以打开模块。然后将以下代码复制并粘贴到空白模块中:
    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
    

    注意:在上面的代码中,“状态”是创建下拉列表时使用的标题名称,“已完成”“进行中”“未开始”是下拉列表项。您可以根据需要更改这些值及其对应的颜色。

    VBA window with ThisDocument selected, showing code pasted into the blank module
  9. 保存并关闭代码窗口。现在,当您从下拉列表中选择一个选项时,相应的颜色将填充单元格。
    Demo showing a cell's color changing when an item is selected from the drop-down list

最佳办公生产力工具

Kutools for Word - 通过超过 100 个卓越功能提升您的 Word 体验!

🤖 Kutools AI 功能AI助手 / 实时助手 / 超级润色(保留格式)/ 超级翻译(保留格式)/ AI遮挡 / AI校正...

📘 文档精通拆分页面 / 合并文档 / 以多种格式导出选择内容(PDF/TXT/DOC/HTML...)/ 批量转换为 PDF...

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

🧹 轻松清理:清除多余空格 / 分节符 / 文本框 / 超链接 / 更多清理工具,请前往“清除”组...

创意插入:插入千位分隔符 / 复选框 / 单选按钮 / 二维码 / 条形码 / 多张图片 / 在“插入 ”组中发现更多...

🔍 精确选择:定位特定页面 / 表格 / 形状 / 标题段落 / 使用更多 选择 功能增强导航...

星级增强功能跳转到任意位置 / 自动插入重复文本 / 在文档窗口之间切换 / 11 转换 工具...

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想尝试这些功能吗?立即下载 Kutools for Word!🚀
 

最佳办公生产力工具

Kutools for Word - 100+ Word 工具