Skip to main content

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

Author: Xiaoyang Last Modified: 2025-08-06

假設您的 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 -讓你的 Word進階,擁有超過 100 項強大功能!

🤖 Kutools AI FeaturesAI Assistant / 即時助手 / 超級潤色(保留格式) / 超級翻譯(保留格式) / AI遮擋 / AI校正...

📘 文檔掌控文檔拆分頁面 / 文檔合併 / 選取內容多格式導出(PDF/TXT/DOC/HTML...)/ 批量轉換為 PDF...

內容編輯跨多個文件批量查找與替換 / 圖片大小統一調整 / 行列翻轉表格 / 表格轉文本...

🧹 潔淨無憂:一鍵清除多餘空格 / 分節符 /文字方塊 / 超鏈接 / 更多清除工具請至 清除所有 群組...

創新插入:插入千位分隔符 / 複選框 / 選項按鈕 / 二維碼 / 條形碼 / 批量插入圖片 / 更多插入功能請前往 插入 群組...

🔍 精準選取:鎖定特定頁面 / 表格 / 形狀 / 標題段落 / 使用更多 選取 功能提升導航效率...

優質強化輕鬆跳轉至文件夾 / 自動插入重複文字 / 切換文檔窗口 /11 轉換工具...

🌍 支援40+ 種語言:可用您的偏好語言體驗 Kutools —— 支援英語、西班牙語、德語、法語、中文等超過40 種語言!

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想要體驗這些功能嗎?立即下載 Kutools for Word!🚀
 

最佳辦公效率工具

Kutools for Word - 100+ Word 工具