提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

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

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

doc color编码下拉

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


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

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

1。 在表格中选择要插入下拉列表的单元格,然后单击 开发人员 > 下拉列表内容控制 图标,看截图:

doc color coded下拉1

2。 将下拉列表插入特定单元格,然后单击 开发人员 > 楼盘,看截图:

doc color coded下拉2

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

(1。)在标题中输入标题名称 头衔 文本框;

(2。)单击 添加 按钮进入 添加选择 对话;

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

doc color coded下拉3

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

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

doc color coded下拉4

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

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

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 color coded下拉5

注意:在上面的代码中, 当前状态 是创建下拉列表时的标题名称,和 完成, 处理中, 不开始 是下拉列表中的项目,您可以将它们更改为您自己的项目。 您还可以根据需要更改颜色。

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

doc color编码下拉




推荐的Word生产力工具

Kutools For Word - 超过100 Word的高级功能,节省您的50%时间

  • 复杂和重复的操作可以在几秒钟内完成一次性处理。
  • 一次将多个图像跨文件夹插入Word文档。
  • 将多个文件夹合并并组合成一个具有所需订单的文件。
  • 根据标题,分节符或其他标准将当前文档拆分为单独的文档。
  • 在Doc和Docx,Docx和PDF之间转换文件,用于常见转换和选择的工具集合等等......
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Jericho · 1 months ago
    Can I color the entire corresponding row instead of just a cell?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 months ago
      Hello, Jericho,
      The below code can help you to deal with your problem, please try it:(You can set the RGB color to your need)

      Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
      With ContentControl.Range
      If ContentControl.Title = "Status" Then
      Select Case .Text
      Case "Complete"
      .Rows.Shading.BackgroundPatternColor = RGB(255, 0, 0)
      Case "In Progress"
      .Rows.Shading.BackgroundPatternColor = RGB(0, 255, 64)
      Case "Not Start"
      .Rows.Shading.BackgroundPatternColor = RGB(0, 0, 255)
      Case Else
      .Rows.Shading.BackgroundPatternColor = wdColorAutomatic
      End Select
      End If
      End With
      End Sub
  • To post as a guest, your comment is unpublished.
    Raj · 4 months ago
    Instead of colors, could we use symbols? like if 'completed' display symbol with character code 252, if not started then symbol with character code 88 etc. can you share the vba code using symbols as display plz?
  • To post as a guest, your comment is unpublished.
    Patti · 7 months ago
    Could I modify this code to have it highlight any drop down choice without listing all of them out?
  • To post as a guest, your comment is unpublished.
    Shannon · 7 months ago
    Hi, Thank you for this. Is there a way of only changing the text color and not the entire cell?
    • To post as a guest, your comment is unpublished.
      skyyang · 7 months ago
      Hi, Shannon,
      To change the text color instead of the background color, the below VBA code can help you, please try it, hope it can help you!

      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).Range.Font.Color = wdColorRed
      Case "In Progress"
      .Cells(1).Range.Font.Color = wdColorGreen
      Case "Not Start"
      .Cells(1).Range.Font.Color = wdColorBlue
      Case Else
      .Cells(1).Range.Font.Color = wdColorAutomatic
      End Select
      End If
      End With
      End Sub
      • To post as a guest, your comment is unpublished.
        Bryan Rantala · 7 months ago
        Struggling here to apply this as a style rather than a color or shade.
  • To post as a guest, your comment is unpublished.
    mark · 9 months ago
    Could I enter custom colors? (rgb)
  • To post as a guest, your comment is unpublished.
    Paul · 9 months ago
    this worked grate! Thanks. I tried to apply same code to another drop down content box in same document and I'm having trouble. Don't know how to get tweo in the VBA window. I get 'compile error, Ambiguous name detected: Document_ContentControlOnExit'
  • To post as a guest, your comment is unpublished.
    Z · 10 months ago
    When I exit and reopen the program, I have to reenter the VBA code. How do I make it so that it saves?
    • To post as a guest, your comment is unpublished.
      skyyang · 10 months ago
      Hello,
      To save the vba code when opening the file next time, you should save the word file as Word Macro-Enabled Document format. Please try it, hope it can help you!