跳到主要內容

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

假設我的Word文檔中有一個表,現在,我想在表的列中插入顏色編碼的下拉列表。 這意味著當我從下拉菜單中選擇一個選項時,單元格顏色變為紅色,而當我在下拉菜單中選擇另一選項時,單元格顏色變為綠色,如下面的屏幕截圖所示。 您如何在Word文檔中解決這項工作?

doc顏色編碼的下拉列表

使用VBA代碼在Word文檔中插入顏色編碼的下拉列表


使用VBA代碼在Word文檔中插入顏色編碼的下拉列表

以下步驟可以幫助您根據需要完成此任務,首先,插入下拉列表,然後為下拉列表應用顏色。 請這樣做:

1。 在表格中選擇要插入下拉列表的單元格,然後單擊 開發者 > 下拉列表內容控制 圖標,請參見屏幕截圖:

doc顏色編碼下拉菜單1

2。 下拉列表插入到特定的單元格,然後單擊 開發者 > 氟化鈉性能,請參見屏幕截圖:

doc顏色編碼下拉菜單2

3。 在 內容控制屬性 對話框,請執行以下操作:

(1.)將標題名稱輸入到 稱呼 文本框;

(2.)點擊 加入 按鈕轉到 添加選擇 對話;

(3.)在 添加選擇 對話框中,將下拉列表項輸入到 顯示名稱 文本框。

doc顏色編碼下拉菜單3

4. 重複步驟3,根據需要插入其他下拉列表項。

5。 創建第一個下拉列表後,您可以根據需要將其複制並粘貼到其他單元格中。 看截圖:

doc顏色編碼下拉菜單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顏色編碼下拉菜單5

備註:在上面的代碼中, Status 是創建下拉列表時的標題名稱,並且 完成, 進行中, 不開始 是下拉列表中的項目,您可以將其更改為自己的項目。 您還可以根據需要更改顏色。

8。 然後保存並關閉代碼窗口,現在,當您從下拉列表中選擇一項時,其相對顏色將被單元格填充,請參見屏幕截圖:

doc顏色編碼的下拉列表


 

最佳辦公生產力工具

Kutools for Word - 透過 Over 提升您的文字體驗 100 顯著特點!

🤖 Kutools 人工智慧助手:用人工智慧改變你的寫作 - 生成內容  /  重寫文字  /  總結文件  /  查詢資料 基於文檔,全部在Word中

📘 文件掌握: 分頁  /  合併文件  /  以各種格式匯出選擇(PDF/TXT/DOC/HTML...)  /  批次轉換為PDF  /  將頁面匯出為圖像  /  一次列印多個文件...

內容編輯: 批量查找和替換 跨多個文件  /  調整所有圖片的大小  /  轉置表行和列  /  將表格轉換為文字...

🧹 輕鬆清潔: 移開 多餘的空間  /  分節符  /  所有標題  /  文本框  /  超鏈接  / 如需更多拆卸工具,請前往我們的 刪除組...

創意插入: 插入 千位分隔符  /  複選框  /  單選按鈕  /  QR Code  /  條碼  /  對角線表  /  公式標題  /  圖片說明  /  表格標題  /  多張圖片  / 發現更多 插入組...

🔍 精準選擇:精確定位 特定頁面  /    /  形狀  /  標題段落  / 增強導航功能 更多 選擇功能...

星級增強: 快速導航至任何位置  /  自動插入重複文字  /  在文檔視窗之間無縫切換  /  11 轉換工具...

???? 想嘗試這些功能嗎? Kutools for Word 提供了 60天免費試用,沒有任何限制! 🚀
 

 

Comments (54)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
hello . i tried but it not go.
This comment was minimized by the moderator on the site
bonjour, je ne parviens pas à enregistrer
This comment was minimized by the moderator on the site
I have a word document that i would like some help with, in short, i have a large inspection table, items either comply, don't, further information etc. I currently use a simple drop-down option which has 5-6 options, ill like a selection option that when different options are selected, that changes the cells/row to a red or green etc, and then also (If possible) updates a Summary Table? is this possible, I'll pay someone for a general templet i can the work on....

Thank you in advance....
This comment was minimized by the moderator on the site
Hello, peter,
Did you mean to select an item from the drop down list, and a specific background color is filled for that row?
Please view the attachment to check if it is your need. If this is not your need, please give your problem more clearly.
Thank you!
This comment was minimized by the moderator on the site
Almost, by changing the drop-down tab the whole row changes color, perfect, thank you, however the magic is then coping the row (Yet only rows that don't comply or need further info etc... ) and placing it in a summary table at the top of the page in another table etc... getting them all to keep adding would be great in a summary table.

I can then alter the summary table as things change, and it updated the main parent table .... I'm not sure this is possible.

I'm happy to have a Teams Meeting to get this working, it would be apricated as it is doing my head in...

This comment was minimized by the moderator on the site
Hi, this is great, got me over a major problem with my fillable form. However, when I go to protect my fillable form, it brings up the debugger box? any idea how i can overcome this?
This comment was minimized by the moderator on the site
Thank you, i have managed to change the background colour. Is there a way i can change the text to white?
This comment was minimized by the moderator on the site
Hello, Emily,
To change the text color to white, please apply the following code:
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
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "In Progress"
                .Cells(1).Shading.BackgroundPatternColor = wdColorGreen
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "Not Start"
                .Cells(1).Shading.BackgroundPatternColor = wdColorBlue
                .Cells(1).Range.Font.Color = wdColorWhite
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
What code do you add so it goes back to the normal colours (both background and text) when you select "Choose an item" from dropdown list after previously selecting a case
This comment was minimized by the moderator on the site
Hello, Usama,
In this case, the code in this article can help you. When uou select an item and then do back to choosing "Choose an item", the backdround color will not be filled.
Please try again. Thank you!
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Hi I want a drop down list of colours but i do not want any text in the cells, i just want my users to be able to choose a colour as I am creating a RAG rated form.

is this possible?

thanks.
This comment was minimized by the moderator on the site
Probably way too late, but in case anyone else is wondering, you can probably do something like this by just adapting the code a bit.

adjust the range.font.color line with the same wd color code as the code above. Its not perfect but if the font is the same color as the highlight, you wont be able to visually see the difference
This comment was minimized by the moderator on the site
I have pasted the code and can see my drop down options (the text) but when I choose them, the background colors don't apply. Do you have any way to help me figure out what I may be doing wrong? Please and thank you! This is exactly what I'm hoping to do in my document, if I can make it work. Thanks!
This comment was minimized by the moderator on the site
Hello, Susan
You should check if the corresponding text in the VBA code has been mofified to your own contents.

Note: In the above code, Status is the title name when you creating the drop down list, and Complete, In Progress, Not Start are the items of the drop down list, you can change them to your own. And you can also change the color to your need.

If there is still problem after changing the text, you can upload the attachment word file and we will help you check it.
Thank you!
This comment was minimized by the moderator on the site
I am having the same issue.
I have changed the titles names in the code and followed the rest of the instructions.
Can you please tell me what I may have done wrong?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations