Note: The other languages of the website are Google-translated. Back to English
Se connecter  \/ 
x
or
x
S'inscrire  \/ 
x

or

Comment insérer une liste déroulante à code couleur dans un tableau Word?

Supposons que j'ai un tableau dans mon document Word et que maintenant, je souhaite insérer une liste déroulante à code couleur dans une colonne du tableau. Cela signifie que lorsque je sélectionne une option dans la liste déroulante, la couleur de la cellule devient rouge et lorsque je sélectionne une autre option dans la liste déroulante, la couleur de la cellule devient verte comme illustré ci-dessous. Comment pourriez-vous résoudre ce travail dans un document Word?

doc déroulant à code couleur

Insérer une liste déroulante à code couleur dans un document Word avec le code VBA


Insérer une liste déroulante à code couleur dans un document Word avec le code VBA

Les étapes suivantes peuvent vous aider à terminer cette tâche selon vos besoins, tout d'abord, insérez la liste déroulante, puis appliquez la couleur de la liste déroulante. Veuillez faire comme ceci:

1. Sélectionnez une cellule du tableau dans laquelle vous souhaitez insérer la liste déroulante, puis cliquez sur Développeur > Contrôle du contenu de la liste déroulante icône, voir capture d'écran:

doc code couleur déroulant 1

2. La liste déroulante est insérée dans la cellule spécifique, puis cliquez sur Développeur > Propriétés, voir capture d'écran:

doc code couleur déroulant 2

3. Dans le Propriétés du contrôle du contenu boîte de dialogue, veuillez effectuer les opérations suivantes:

(1.) Entrez le nom du titre dans le Titre de propriété zone de texte;

(2.) Cliquez sur Ajouter bouton aller au Ajouter un choix dialogue;

(3.) Dans le Ajouter un choix boîte de dialogue, saisissez l'élément de la liste déroulante dans le Nom d'affichage zone de texte.

doc code couleur déroulant 3

4. Répétez l'étape 3 pour insérer d'autres éléments de liste déroulante selon vos besoins.

5. Après avoir créé la première liste déroulante, vous pouvez la copier et la coller dans d'autres cellules selon vos besoins. Voir la capture d'écran:

doc code couleur déroulant 4

6. Ensuite, vous devez appliquer un code VBA, maintenez la touche ALT + F11 clés pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

7. Dans le Microsoft Visual Basic pour applications fenêtre, double-cliquez Ce document du Projet-Projet volet pour ouvrir le mode, puis copiez et collez le code suivant dans le module vide.

Code VBA: Insérez la liste déroulante à code couleur dans le tableau d'un document 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 code couleur déroulant 5

Notes: Dans le code ci-dessus, Statut est le nom du titre lorsque vous créez la liste déroulante, et Complété, En Cours, Ne démarre pas sont les éléments de la liste déroulante, vous pouvez les modifier par vous-même. Et vous pouvez également changer la couleur selon vos besoins.

8. Ensuite, enregistrez et fermez la fenêtre de code, maintenant, lorsque vous sélectionnez un élément dans la liste déroulante, sa couleur relative sera remplie avec la cellule, voir capture d'écran:

doc déroulant à code couleur



Outils de productivité Word recommandés

 

Kutools For Word - Plus de 100 fonctionnalités avancées pour Word, économisez 50% de votre temps

  • Les opérations compliquées et répétées peuvent être effectuées une seule fois en quelques secondes.
  • Insérez simultanément plusieurs images dans des dossiers dans un document Word.
  • Fusionner et combiner plusieurs fichiers Word dans des dossiers en un seul avec l'ordre souhaité.
  • Divisez le document actuel en documents séparés selon l'en-tête, le saut de section ou d'autres critères.
  • Convertissez des fichiers entre Doc et Docx, Docx et PDF, collection d'outils pour les conversions et la sélection courantes, etc.
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.
    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!