Gå til hovedindhold

Sådan indsætter du en farvekodet rulleliste i Word bord?

Antag, at jeg har et bord i min Word dokument, og nu vil jeg indsætte en farvekodet rulleliste i en kolonne i tabellen. Det betyder, at når jeg vælger en mulighed fra rullemenuen, bliver cellefarven rød, og når jeg vælger en anden mulighed i rullemenuen, bliver cellefarven grøn som vist på følgende skærmbillede. Hvordan kunne du løse dette job i Word dokumentere?

doc farvekodet drop down

Indsæt farvekodet rulleliste i Word dokument med VBA-kode


Indsæt farvekodet rulleliste i Word dokument med VBA-kode

De følgende trin kan hjælpe dig med at afslutte denne opgave efter behov, først indsæt rullelisten og derefter anvende farven til rullemenuen. Gør som dette:

1. Vælg en celle i tabellen, hvor du vil indsætte rullemenuen, og klik derefter på Udvikler > Indholdskontrol i rullelisten ikon, se skærmbillede:

drop-down drop-down farvefarve 1

2. Rullelisten indsættes i den specifikke celle, og klik derefter på Udvikler > Ejendomme, se skærmbillede:

drop-down drop-down farvefarve 2

3. I Egenskaber til indholdskontrol dialogboksen, skal du udføre følgende handlinger:

(1.) Indtast titelnavnet i Titel tekstboks;

(2.) Klik på Tilføj knappen gå til Tilføj valg dialog;

(3.) I Tilføj valg dialog, skal du skrive rullelisteelementet i Visningsnavn tekstboks.

drop-down drop-down farvefarve 3

4. Gentag trin 3 for at indsætte andre rullelisteelementer efter behov.

5. Når du har oprettet den første rulleliste, kan du kopiere og indsætte den i andre celler efter behov. Se skærmbillede:

drop-down drop-down farvefarve 4

6. Derefter skal du anvende en VBA-kode, hold nede ALT + F11 nøgler til at åbne Microsoft Visual Basic til applikationer vindue.

7. I Microsoft Visual Basic til applikationer dobbeltklik på vinduet Dette dokument fra Projekt-projekt rude for at åbne tilstanden og derefter kopiere og indsætte følgende kode i det tomme modul.

VBA-kode: Indsæt farvekodet rulleliste i en tabel med en Word dokument:

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

drop-down drop-down farvefarve 5

Bemærk: I ovenstående kode, Status er titelnavnet, når du opretter rullelisten, og Komplet, I gang, Ikke start er emnerne på rullelisten, kan du ændre dem til dine egne. Og du kan også ændre farven efter dit behov.

8. Gem og luk derefter kodevinduet, nu når du vælger et element fra rullelisten, udfyldes dets relative farve med cellen, se skærmbillede:

doc farvekodet drop down


 


Anbefales Word Produktivitetsværktøjer

 

Kutools For Word - Mere end 100 avancerede funktioner til Word, Spar din 50 % tid

  • Komplicerede og gentagne operationer kan udføres engangsbehandling på få sekunder.
  • Indsæt flere billeder på tværs af mapper i Word dokument med det samme.
  • Flet og kombiner flere Word filer på tværs af mapper i én med din ønskede rækkefølge.
  • Opdel det aktuelle dokument i separate dokumenter efter overskrift, sektionsskift eller andre kriterier.
  • Konverter filer mellem Doc og Docx, Docx og PDF, samling af værktøjer til almindelige konverteringer og markering osv. ...

 

Comments (51)
Rated 5 out of 5 · 1 ratings
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
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?
This comment was minimized by the moderator on the site
Hi, is there any way to color code-vba the text in a dropdown if it is not in a table or cell? Thanks!
This comment was minimized by the moderator on the site
Hello, nikki
If you just want to color coded the values from the drop down list, maybe the following code can help you:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
  If .Title = "Status" Then
    Select Case .Range.Text
      Case "Complete": .Range.Font.ColorIndex = wdRed
      Case "In Progress": .Range.Font.ColorIndex = wdBlue
      Case "Not Start": .Range.Font.ColorIndex = wdGreen
      Case Else: .Range.Font.ColorIndex = wdAuto
    End Select
  End If
End With
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Hi, is there way to change the color of the text in the dropdown if it is not in a table or cell?

Thanks!
This comment was minimized by the moderator on the site
Hi
This is so simple I really like it.

Is there any way to do the same but with an inserted word shape, (say oval). Obviously it's not .Cell ?

Many thanks
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi I tried this code however it is not working, could you please let me know which part is wrong in here

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 = RGB(255, 0, 0)
Case "Delayed"
.Cells(1).Shading.BackgroundPatternColor = RGB(0, 255, 64)
Case "On Track"
.Cells(1).Shading.BackgroundPatternColor = RGB(0, 0, 255)
Case "Off Track"
.Cells(1).Shading.BackgroundPatternColor = RGB(0, 0, 25)
Case "FLAG"
.Cells(1).Shading.BackgroundPatternColor = RGB(10, 0, 255)
Case Else
.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End With
End Sub
This comment was minimized by the moderator on the site
If you are using the File Properties Metadata "Status" field in your document, use "Status1" for lists that require FONT colour changes only; and use "Status2" for lists whereby the background FILL colour is happening, and FONT colour changes are needed (e.g., White on Red, White on Black, Black on Pale Yellow). How to achieve BOLD is it the following appended to the code?
If Selection.Type = wdSelectionNormal Then
Selection.Range.Bold = wdToggle
End If

Question: After running the code, then realizing it was one-time use because my file was a Word.doc; I converted file to macro-enabled. Now I see all the Control start/end flags. How to turn off with within the code itself or otherwise?
This comment was minimized by the moderator on the site
If you are using the File Properties Metadata "Status" field in your document, use "Status1" for lists that require FONT colour changes only; and use "Status2" for lists whereby the background FILL colour is happening, and FONT colour changes are needed (e.g., White on Red, White on Black, Black on Pale Yellow).  How to achieve BOLD is it the following appended to the code? If Selection.Type = wdSelectionNormal Then
Selection.Range.Bold = wdToggle
End If

Question:  After running the code, then realizing it was one-time use because my file was a Word.doc; I converted file to macro-enabled. Now I see all the Control start/end flags.  How to turn off with within the code itself or otherwise?
This comment was minimized by the moderator on the site
I can get the colours to work with the dropdown list but only after clicking the "status" tag.  Can the staus tag dissapear? As I think it doesnt look nice when the document is completed
This comment was minimized by the moderator on the site
Hello,
Can you color code the next column, instead of the drop-down list column? For example, no color code in "Status" column (just choices), but color code in adjacent column? Thanks.
This comment was minimized by the moderator on the site
Awesome instruction - worked perfectly! Thank you!
This comment was minimized by the moderator on the site
This was super helpful and I implemented it successfully. I'm wondering how I can also change the text colour on the same row I have already changed the background colour. Eg. the background row colour is red, but I want the text to be white not black?
This comment was minimized by the moderator on the site
the originally post code worked great but i want to *** it to another drop down box, when i try and *** the same code to VB with a change to the value in the "status" section of the following line:
If ContentControl.Title = "Status" Then
it does not work?
This comment was minimized by the moderator on the site
Hello, joni,If there are two or more tables of drop down lists, you just need to copy and paste the below code into the original code, and change the text reference to your need.
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


The complete code should be this:<div data-tag="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
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
If ContentControl.Title = "Name" Then
Select Case .Text
Case "Lucy"
.Cells(1).Shading.BackgroundPatternColor = wdColorOrange
Case "Skyyang"
.Cells(1).Shading.BackgroundPatternColor = wdColorYellow
Case "Ruby"
.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case Else
.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End With
End SubPlease try, hope it can help you!
This comment was minimized by the moderator on the site
This worked fantastic, thank you for your help.
I am also try to figure out a method for the following:
I have two cells that have a numerical value. In a separate celI i want to multiply these values, if the result falls within three set numerical ranges i would like the cell to display text based on the range values.
If the risk severity of a hazard is 3
This comment was minimized by the moderator on the site
Hello - thanks very much for this. How do I incorporate code to change the font color based on selection from drop down list.
This comment was minimized by the moderator on the site
Hello,

I tried to create two color code drop down lists in one Word template, however, I get an error message saying 'Ambiguous name detected: Document_ContentControON EXIT. Could you please inform me what I have done wrong?




Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

With ContentControl.Range

If ContentControl.Title = "Corrective action" Then

Select Case .Text

Case "Corrective action necessary"

.Cells(1).Shading.BackgroundPatternColor = wdColorRed

Case "No further action needed"

.Cells(1).Shading.BackgroundPatternColor = wdColorGreen

Case "Corrective action recommended"

.Cells(1).Shading.BackgroundPatternColor = wdColorYellow

Case Else

.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic

End Select

End If

End With

End Sub

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

With ContentControl.Range

If ContentControl.Title = "Corrective action for cd" Then

Select Case .Text

Case "Use a new Cd curve"

.Cells(1).Shading.BackgroundPatternColor = wdColorRed

Case "Use an existing Cd curve"

.Cells(1).Shading.BackgroundPatternColor = wdColorGreen

Case "Check the setting and Probe"

.Cells(1).Shading.BackgroundPatternColor = wdColorYellow

Case Else

.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic

End Select

End If

End With


Many thanks in advance for your support.
This comment was minimized by the moderator on the site
Thank you for providing this it is so helpful! However I have come across a bug. When I create a new row in the table below a cell which has had the Drop Down list, it copies the colour of the above cell into this cell without the Drop Down List. If I then copy and paste the drop down list and change the option, it does not change the colour of the cell. Can anyone help?
This comment was minimized by the moderator on the site
Hi, I've followed the steps a few times over but the colours don't appear at all?
This comment was minimized by the moderator on the site
Thank you so much for sharing this! However, I'm running into a weird quirk. Sometimes, when I select an item from a drop-down menu and then click elsewhere in the document for it to apply the formatting, it does not work. It selects the proper word from the drop-down menu, but it just stays normal, black text. I have to undo back to before I selected anything, re-select the item from the drop-down menu, and then ONLY click in another drop-down menu to get the formatting to apply. Any ideas on why it's doing this and how I can fix it?
This comment was minimized by the moderator on the site
Hello, is there a way to do multiple color-coded drop downs within one document? Thank you!
This comment was minimized by the moderator on the site
Hi, awesome!!! But is there a way to change the color / text of another cell in the same row but in different column? For example: instead of changing the background of "Complete", change the background of "Project-001" . Help me please. Thanks in advance
This comment was minimized by the moderator on the site
Hi, Jaimes,
To solve your problem, please apply the below code:
Note: In the code,the number 1 in the script Cells(1) is the column number of the table, you can chnage it tou 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(1).Cells(1).Shading.BackgroundPatternColor = wdColorRed
Case "In Progress"
.Rows(1).Cells(1).Shading.BackgroundPatternColor = wdColorGreen
Case "Not Start"
.Rows(1).Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case Else
.Rows(1).Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End With
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Can I color the entire corresponding row instead of just a cell?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hi, this is fantastic! Is there a way to choose from lots of colours?
This comment was minimized by the moderator on the site
Hi, Sam,
To apply more color you like, you should use the below code, and change 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"
                .Cells(1).Shading.BackgroundPatternColor = RGB(255, 0, 0)
            Case "In Progress"
                .Cells(1).Shading.BackgroundPatternColor = RGB(0, 255, 64)
            Case "Not Start"
                .Cells(1).Shading.BackgroundPatternColor = RGB(0, 0, 255)
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub


Please try, hope it can help you!
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
Could I modify this code to have it highlight any drop down choice without listing all of them out?
This comment was minimized by the moderator on the site
Hi, Thank you for this. Is there a way of only changing the text color and not the entire cell?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Struggling here to apply this as a style rather than a color or shade.
This comment was minimized by the moderator on the site
Could I enter custom colors? (rgb)
This comment was minimized by the moderator on the site
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'
This comment was minimized by the moderator on the site
When I exit and reopen the program, I have to reenter the VBA code. How do I make it so that it saves?
This comment was minimized by the moderator on the site
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!
This comment was minimized by the moderator on the site
Hi there I have just successfully created the drop down list and colours as advised. i have tried to add a further row so i have 4 vs 3 drop downs. I cut and pasted and changed but no colour shows when i select this option, any ideas how to fix?
This comment was minimized by the moderator on the site
Hello, Peter,
First, you should create the drop down list into the table, and then *** your fourth drop down text into the code as below:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl.Range
If ContentControl.Title = "Status" Then
Select Case .Text
Case "Complete" 'First drop down item
.Cells(1).Shading.BackgroundPatternColor = wdColorRed
Case "In Progress" 'Second drop down item
.Cells(1).Shading.BackgroundPatternColor = wdColorGreen
Case "Not Start" 'Third drop down item
.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case "New Task" 'Fourth drop down item
.Cells(1).Shading.BackgroundPatternColor = wdColorYellow
Case Else
.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End With
End Sub


Please try, hope it can help you!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations