Skip to main content

如何在 Word 表格中插入色彩編碼的下拉列表?

Author: Xiaoyang Last Modified: 2025-05-12

假設您的 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
    

    注意:在上述代碼中,「Status」是創建下拉列表時使用的標題名稱,而「Complete」、「In Progress」和「Not Start」是項目。您可以根據需要更改這些值及相應的顏色。

    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 工具