Passer au contenu principal

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 > biens, 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 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 du profil 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 !, 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


 

Meilleurs outils de productivité bureautique

Kutools for Word - Élevez votre expérience Word avec Over 100 Caractéristiques remarquables !

🤖 Assistant IA Kutools: Transformez votre écriture avec l'IA - Générer du contenu  /  Réécrire le texte  /  Résumer des documents  /  Demander des informations basé sur le document, le tout dans Word

(I.e. Maîtrise des documents: Pages divisées  /  Fusionner des documents  /  Exporter la sélection dans différents formats (PDF/TXT/DOC/HTML...)  /  Conversion par lots en PDF  /  Exporter des pages sous forme d'images  /  Imprimer plusieurs fichiers à la fois

Édition du contenu: Rechercher et remplacer par lots sur plusieurs fichiers  /  Redimensionner toutes les images  /  Transposer les lignes et les colonnes du tableau  /  Convertir le tableau en texte

🧹 Nettoyage sans effort : balayer Espaces supplémentaires  /  Sauts de section  /  Tous les en-têtes  /  Boîtes de texte  /  Liens hypertextes  / Pour plus d'outils de suppression, rendez-vous sur notre Supprimer le groupe

Insertions créatives: Insérer Mille séparateurs  /  Cases à cocher  /  Boutons radio  /  code QR  /  Code barres  /  Tableau des lignes diagonales  /  Légende de l'équation  /  Légende  /  Légende de la table  /  Plusieurs images  / Découvrez-en davantage dans le Insérer un groupe

🔍 Sélections de précision: Localiser pages spécifiques  /  les tables  /  formes  /  paragraphes de titre  / Améliorez la navigation avec PLUS Sélectionnez les fonctionnalités

Améliorations des étoiles: Naviguez rapidement vers n’importe quel endroit  /  insertion automatique de texte répétitif  /  basculer de manière transparente entre les fenêtres de document  /  11 outils de conversion

👉 Vous voulez essayer ces fonctionnalités ? Kutools for Word propose un Essai gratuit 60-day, sans aucune limite ! 🚀
 

 

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