Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

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

Author Xiaoyang Last modified

假設您的 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 FeaturesAI AssistantReal-Time AssistantSuper Polish(保留格式)Super Translate(保留格式)AI RedactionAI Proofread...

📘 文件掌握分割頁面文檔合併區域導出成各種格式(PDF/TXT/DOC/HTML...)批量轉換為 PDF...

正文編輯批量查找與替換多個文件批量調整所有圖片大小翻轉表格的行與列表格轉文本...

🧹 輕鬆清理:快速清除多餘空格分節符文字框超連結/更多清除工具請前往刪除工具組...

創意插入:插入千位分隔符複選框選項按鈕二維碼條形碼批量插入圖片/還有更多精彩功能在插入工具組...

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

亮點增強導航至任何區域自動插入重複文字切換文檔窗口11 轉換工具...

🌍 支援40+種語言:可用 Kutools於您偏好語言環境——支援英語、西班牙語、德語、法語、中文以及超過40種語言!

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

✨ Kutools for Office——一次安裝,五大強力工具!

包含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint

📦 五大工具一次全擁有 | 🔗 與 Microsoft Office 無縫整合 | ⚡ 即刻省時提高工作效率

最佳辦公效率工具

Kutools for Word - 100+ 款 Word 工具