Note: The other languages of the website are Google-translated. Back to English
Accedi  \/ 
x
or
x
Registrati  \/ 
x

or

Come inserire un elenco a discesa con codice colore nella tabella di Word?

Supponiamo di avere una tabella nel mio documento di Word e ora voglio inserire un elenco a discesa con codice colore in una colonna della tabella. Significa che quando seleziono un'opzione dal menu a discesa, il colore della cella diventa rosso e quando seleziono un'altra opzione nel menu a discesa, il colore della cella diventa verde come mostrato nella seguente schermata. Come hai potuto risolvere questo lavoro nel documento di Word?

doc a discesa con codice colore

Inserisci un elenco a discesa con codice colore nel documento Word con codice VBA


Inserisci un elenco a discesa con codice colore nel documento Word con codice VBA

I seguenti passaggi possono aiutarti a completare questa attività di cui hai bisogno, prima inserisci l'elenco a discesa, quindi applica il colore per il menu a discesa. Per favore, fai come segue:

1. Seleziona una cella nella tabella in cui desideri inserire il menu a discesa, quindi fai clic su Costruttori > Controllo contenuto elenco a discesa icona, vedi screenshot:

elenco a discesa con codice colore doc 1

2. Il menu a discesa viene inserito nella cella specifica e quindi fare clic Costruttori > Proprietà, vedi screenshot:

elenco a discesa con codice colore doc 2

3. Nel Proprietà di controllo del contenuto finestra di dialogo, eseguire le seguenti operazioni:

(1.) Immettere il nome del titolo nel file Titolo casella di testo;

(2.) Fare clic su Aggiungi pulsante vai al Aggiungi scelta dialogo;

(3.) Nella Aggiungi scelta finestra di dialogo, digitare l'elemento dell'elenco a discesa nel file Display Nome casella di testo.

elenco a discesa con codice colore doc 3

4. Ripeti il ​​passaggio 3 per inserire altri elementi dell'elenco a discesa di cui hai bisogno.

5. Dopo aver creato il primo elenco a discesa, puoi copiarlo e incollarlo in altre celle di cui hai bisogno. Vedi screenshot:

elenco a discesa con codice colore doc 4

6. Quindi dovresti applicare un codice VBA, tieni premuto il tasto ALT + F11 chiavi per aprire il Microsoft Visual Basic, Applications Edition finestra.

7. Nel Microsoft Visual Basic, Applications Edition finestra, fare doppio clic Questo documento dal Progetto-Progetto riquadro per aprire la modalità, quindi copia e incolla il codice seguente nel modulo vuoto.

Codice VBA: inserisci un elenco a discesa con codice colore nella tabella di un documento di 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

elenco a discesa con codice colore doc 5

Note:: Nel codice sopra, Stato è il nome del titolo quando si crea l'elenco a discesa e Completare, In corso, Non iniziato sono gli elementi dell'elenco a discesa, è possibile modificarli con i propri. E puoi anche cambiare il colore secondo le tue necessità.

8. Quindi salva e chiudi la finestra del codice, ora, quando selezioni un elemento dall'elenco a discesa, il suo colore relativo verrà riempito con la cella, vedi screenshot:

doc a discesa con codice colore



Strumenti di produttività Word consigliati

 

Kutools per Word: oltre 100 funzionalità avanzate per Word, risparmia il 50% del tempo

  • Le operazioni complicate e ripetute possono essere eseguite una sola volta in pochi secondi.
  • Inserisci più immagini tra le cartelle nel documento di Word contemporaneamente.
  • Unisci e combina più file di Word tra cartelle in uno con l'ordine desiderato.
  • Suddividi il documento corrente in documenti separati in base a titolo, interruzione di sezione o altri criteri.
  • Converti file tra Doc e Docx, Docx e PDF, raccolta di strumenti per conversioni e selezioni comuni e così via ...
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Geoff Robinson · 2 days ago
    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


  • To post as a guest, your comment is unpublished.
    Thomas · 1 months ago
    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.
  • To post as a guest, your comment is unpublished.
    Denise · 5 months ago
    Awesome instruction - worked perfectly! Thank you!
  • To post as a guest, your comment is unpublished.
    Jenny · 9 months ago
    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?

  • To post as a guest, your comment is unpublished.
    joni watt · 1 years ago
    the originally post code worked great but i want to add it to another drop down box, when i try and add 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?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      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:
      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 Sub
      Please try, hope it can help you!

      • To post as a guest, your comment is unpublished.
        Joni · 1 years ago
        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
  • To post as a guest, your comment is unpublished.
    Kiki · 1 years ago
    Hello - thanks very much for this. How do I incorporate code to change the font color based on selection from drop down list.
  • To post as a guest, your comment is unpublished.
    B.Choi · 1 years ago
    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.
  • To post as a guest, your comment is unpublished.
    Hannah · 1 years ago
    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?
  • To post as a guest, your comment is unpublished.
    SF · 1 years ago
    Hi, I've followed the steps a few times over but the colours don't appear at all?
  • To post as a guest, your comment is unpublished.
    kethryvalis · 2 years ago
    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?
  • To post as a guest, your comment is unpublished.
    Hayley · 2 years ago
    Hello, is there a way to do multiple color-coded drop downs within one document? Thank you!
  • To post as a guest, your comment is unpublished.
    jaimes7 · 2 years ago
    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
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      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!
  • To post as a guest, your comment is unpublished.
    Jericho · 2 years ago
    Can I color the entire corresponding row instead of just a cell?
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      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
  • To post as a guest, your comment is unpublished.
    Raj · 2 years ago
    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?
  • To post as a guest, your comment is unpublished.
    Patti · 2 years ago
    Could I modify this code to have it highlight any drop down choice without listing all of them out?
  • To post as a guest, your comment is unpublished.
    Shannon · 3 years ago
    Hi, Thank you for this. Is there a way of only changing the text color and not the entire cell?
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      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
      • To post as a guest, your comment is unpublished.
        brantala01@gmail.com · 2 years ago
        Struggling here to apply this as a style rather than a color or shade.
  • To post as a guest, your comment is unpublished.
    mark · 3 years ago
    Could I enter custom colors? (rgb)
  • To post as a guest, your comment is unpublished.
    Paul · 3 years ago
    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'
  • To post as a guest, your comment is unpublished.
    Z · 3 years ago
    When I exit and reopen the program, I have to reenter the VBA code. How do I make it so that it saves?
    • To post as a guest, your comment is unpublished.
      skyyang · 3 years ago
      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!
      • To post as a guest, your comment is unpublished.
        Justin Peter Renwick · 1 years ago
        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?
        • To post as a guest, your comment is unpublished.
          skyyang · 1 years ago
          Hello, Peter,
          First, you should create the drop down list into the table, and then add 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!