Passer au contenu principal

Comment appliquer un dégradé de couleur sur plusieurs cellules?

Dans Excel, nous pouvons facilement remplir la couleur d'arrière-plan d'une cellule ou de plusieurs cellules, mais, parfois, nous avons besoin que la couleur soit remplie en dégradé comme illustré ci-dessous, comment pourrait-on obtenir le dégradé de couleur dans une cellule ou sur plusieurs cellules dans Excel?

Dégradé de couleur dans une cellule Dégradé de couleur sur plusieurs cellules
doc dégradé de couleur 1 doc dégradé de couleur 2

Appliquer une couleur de dégradé à une cellule avec la fonctionnalité Format de cellule

Appliquer une couleur dégradée sur plusieurs cellules avec le code VBA


flèche bleue bulle droite Appliquer une couleur de dégradé à une cellule avec la fonctionnalité Format de cellule

Dans Excel, la fonctionnalité Formater les cellules peut vous aider à remplir le dégradé de couleur dans une cellule, procédez comme suit:

1. Sélectionnez une cellule ou plusieurs cellules que vous souhaitez remplir avec un dégradé de couleur chaque cellule, puis cliquez avec le bouton droit pour choisir Format de cellule depuis le menu contextuel, dans le Format de cellule boîte de dialogue, sous la Niveau de remplissage onglet, veuillez cliquer Effets de remplissage bouton, voir capture d'écran:

doc dégradé de couleur 3

2. Dans le Effets de remplissage boîte de dialogue, sélectionnez deux couleurs que vous souhaitez utiliser dans les deux listes déroulantes de Couleurs section, puis sélectionnez un style d'ombrage à votre guise, par exemple horizontal, vertical, etc. Voir la capture d'écran:

doc dégradé de couleur 4

3. Puis clique OK > OK pour fermer les boîtes de dialogue, et la couleur du dégradé est remplie pour chaque cellule comme illustré ci-dessous:

doc dégradé de couleur 5


flèche bleue bulle droite Appliquer une couleur dégradée sur plusieurs cellules avec le code VBA

La méthode ci-dessus peut nous aider à colorer le dégradé dans une cellule individuelle.Si vous devez ombrer le dégradé de couleur sur plusieurs cellules, vous devez appliquer un code VBA pour le résoudre.

1. Tout d'abord, remplissez une couleur d'arrière-plan spécifique dans une plage de cellules.

2. Maintenez le ALT + F11 clés pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

3Cliquez sur insérer > Moduleet collez le code suivant dans le Module Fenêtre.

Code VBA: appliquez une couleur de dégradé sur plusieurs cellules:

Sub colorgradientmultiplecells()
'Updateby Extendoffcie 
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xColor As Long
    Dim I As Long
    Dim K As Long
    Dim xCount As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
LInput:
    Set xRg = Application.InputBox("please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"
        GoTo LInput
    End If
    On Error Resume Next
    Application.ScreenUpdating = False
    xCount = xRg.Rows.Count
    For K = 1 To xRg.Columns.Count
        xColor = xRg.Cells(1, K).Interior.Color
        For I = xCount To 1 Step -1
            xRg.Cells(I, K).Interior.Color = xColor
            xRg.Cells(I, K).Interior.TintAndShade = (xCount - (I - 1)) / xCount
        Next
    Next
End Sub

4. Puis appuyez F5 pour exécuter ce code, et une boîte de dialogue apparaît pour vous rappeler de sélectionner les cellules colorées que vous souhaitez remplir en dégradé de couleur, voir capture d'écran:

 

doc dégradé de couleur 6

5. Et puis cliquez OK bouton, la couleur dans plusieurs cellules s'est affichée en dégradé, voir capture d'écran:

 

doc dégradé de couleur 7

Meilleurs outils de productivité bureautique

🤖 Aide à l'IA Kutools: Révolutionner l'analyse des données en s'appuyant sur : Exécution intelligente   |  Générer du code  |  Créer des formules personnalisées  |  Analyser les données et générer des graphiques  |  Invoquer les fonctions Kutools...
Caractéristiques populaires: Rechercher, mettre en évidence ou identifier les doublons   |  Supprimer les lignes vides   |  Combinez des colonnes ou des cellules sans perdre de données   |   Tour sans formule 
Super recherche: VSearchup à critères multiples    VSearch à valeurs multiples  |   Recherche virtuelle sur plusieurs feuilles   |   Recherche floue ....
Liste déroulante avancée: Créez rapidement une liste déroulante   |  Liste déroulante dépendante   |  Liste déroulante à sélection multiple ....
Gestionnaire de colonnes: Ajouter un nombre spécifique de colonnes  |  Déplacer les colonnes  |  Basculer l'état de visibilité des colonnes masquées  |  Comparer les plages et les colonnes 
Caractéristiques en vedette: Mise au point de la grille   |  Voir Design   |   Grande barre de formule    Gestionnaire de classeurs et de feuilles   |  Centre de ressources (Texte automatique)   |  Sélecteur de date   |  Combiner des feuilles de travail   |  Crypter/déchiffrer les cellules    Envoyer des e-mails par liste   |  Super filtre   |   Filtre spécial (filtre gras/italique/barré...) ...
Les 15 meilleurs ensembles d'outils12 Texte Outils (Ajouter du texte, Supprimer les caractères, ...)   |   50+ Graphique Types (Diagramme de Gantt, ...)   |   40+ Pratique Formules (Calculer l'âge en fonction de l'anniversaire, ...)   |   19 Insertion Outils (Insérer le code QR, Insérer une image à partir du chemin, ...)   |   12 Conversion Outils (Nombres en mots, Conversion des devises, ...)   |   7 Fusionner et fractionner Outils (Lignes de combinaison avancées, Cellules divisés, ...)   |   ... et plus

Améliorez vos compétences Excel avec Kutools for Excel et faites l'expérience d'une efficacité comme jamais auparavant. Kutools for Excel offre plus de 300 fonctionnalités avancées pour augmenter la productivité et gagner du temps.  Cliquez ici pour obtenir la fonctionnalité dont vous avez le plus besoin...

Description


Office Tab apporte une interface à onglets à Office et facilite grandement votre travail

  • Activer l'édition et la lecture par onglets dans Word, Excel, PowerPoint, Publisher, Access, Visio et Project.
  • Ouvrez et créez plusieurs documents dans de nouveaux onglets de la même fenêtre, plutôt que dans de nouvelles fenêtres.
  • Augmente votre productivité de 50% et réduit des centaines de clics de souris chaque jour!
Comments (24)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Thanks for the awesome code.

How do I make the gradient go from colour to white top to bottom?
How would i choose between two colours?
This comment was minimized by the moderator on the site
Hello, Tra,
For the first question, to make the gradient go from color to white top to bottom, please apply the following code:
Sub colorgradientmultiplecells()
'Updateby Extendoffcie
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xColor As Long
Dim I As Long
Dim K As Long
Dim xCount As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
LInput:
Set xRg = Application.InputBox("please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
If xRg.Areas.Count > 1 Then
MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"
GoTo LInput
End If
On Error Resume Next
Application.ScreenUpdating = False
xCount = xRg.Rows.Count
For K = 1 To xRg.Columns.Count
xColor = xRg.Cells(1, K).Interior.Color
For I = xCount To 1 Step -1
xRg.Cells(I, K).Interior.Color = xColor
xRg.Cells(I, K).Interior.TintAndShade = I / xCount
Next
Next
End Sub


For the second question, to fill the gradient with two colors, please apply the belwo code:
Note: to change the two colors, you just need to change the RGB in the code.
Sub colorgradientmultiplecells()
    'Updateby Extendoffcie
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xColor1 As Long
    Dim xColor2 As Long
    Dim I As Long
    Dim K As Long
    Dim xCount As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
        xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
        xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
LInput:
    Set xRg = Application.InputBox("Please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "Does not support multiple selections", vbInformation, "Kutools for Excel"
        GoTo LInput
    End If
    On Error Resume Next
    Application.ScreenUpdating = False
    xCount = xRg.Rows.Count
    xColor1 = RGB(255, 0, 0) ' Red color
    xColor2 = RGB(0, 0, 255) ' Blue color
    For K = 1 To xRg.Columns.Count
        For I = xCount To 1 Step -1
            xRg.Cells(I, K).Interior.Color = RGB( _
                Int((xCount - (I - 1)) / xCount * (xColor2 Mod 256) + (I - 1) / xCount * (xColor1 Mod 256)), _
                Int((xCount - (I - 1)) / xCount * ((xColor2 \ 256) Mod 256) + (I - 1) / xCount * ((xColor1 \ 256) Mod 256)), _
                Int((xCount - (I - 1)) / xCount * (xColor2 \ 65536) + (I - 1) / xCount * (xColor1 \ 65536)))
        Next
    Next
    Application.ScreenUpdating = True
End Sub


Please have a try, hope them can help you!
This comment was minimized by the moderator on the site
Hallo,

Ich habe zu erst den ersten code verwendet, das funktioniert aber leider nicht. der färbt nur einzelne Zellen und die in komplett anderen Farben. Der Code für dunkel oben nach hell unten funktioniert gar nicht bei mir. Da kommt immer ein Syntax Error. Gibt es noch andere möglichkeiten einen Farbverlauf über mehrere Zellen zu erschaffen oder gibt es da eine andere Lösung für mich?

Vielen lieben Dank vorab schon mal.
This comment was minimized by the moderator on the site
Hello, How can I aply this formula for two colors, above I can't watch de solution. Please
This comment was minimized by the moderator on the site
Hello, Can you apply this to a range like 1 to 5 , cells having a number in them, having the 5 as the darkest ?
Would Kutools be able to do a little similar to above , but then arrange a row of numbers into a scale - not good at explaining. Say a survey answering on a scale of 1 to 5 , then graphical show one bar per question showing percent of answers in 1, 2 ,3 ,4 ,5 ; 5% 1's, 10% 2's 15% 3's and 50% 4's , 20% 5's but showing a gradient of colours in a horizontal bar (better if 2 colours).
Thanks , Gord
This comment was minimized by the moderator on the site
Hi, I've tried copying the VBA code but when I try to run it I keep getting a message that says 'Compile Error: Invalid Outside Procedure'....


How do I fix this??


Thanks!
This comment was minimized by the moderator on the site
Hello, how can I go from yellow to red (for example)? It works only from White to an other color. I work with the code from left to right.
This comment was minimized by the moderator on the site
Hello, Jasmin,
Sorry for that, this code only applied to one color, and if you want to fill gradient from left to tight, the below comment has the solution, please check it.
Thank you!
This comment was minimized by the moderator on the site
Hello, Can you apply this to a range like 1 to 5 , cells having a number in them, having the 5 as the darkest ?

Would Kutools be able to do a little similar to above , but then arrange a row of numbers into a scale - not good at explaining. Say a survey answering on a scale of 1 to 5 , then graphical show one bar per question showing percent of answers in 1, 2 ,3 ,4 ,5 ; 5% 1's, 10% 2's 15% 3's and 50% 4's , 20% 5's but showing a gradient of colours in a horizontal bar (better if 2 colours).

Thanks , Gord
This comment was minimized by the moderator on the site
Why I got Black-White color replace my gradient
This comment was minimized by the moderator on the site
Hello! I have the same problem, i choose the colors for my gradient but when applying the code it turns into black to white gradient. Anyy help?? thanks!
This comment was minimized by the moderator on the site
Hello, Piyaphan,
The above code works well in my worksheet, which Excel version do you use?
Or you can give your problem more detailed.
Thank you!
This comment was minimized by the moderator on the site
hey, I'm not that used to VBA codes. How do I set another color in the code?
This comment was minimized by the moderator on the site
Hello, paul,
If you want to set another color, you just need to fill your desired color to the cells, and then apply the above code in this article.
Please try it.
This comment was minimized by the moderator on the site
thanks! it works :)
This comment was minimized by the moderator on the site
How do I get this to have the gradient go left to right?
This comment was minimized by the moderator on the site
Hello, Sean,
To apply the color gradient from left to right, please use the following VBA code:

Sub colorgradientmultiplecells()
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xColor As Long
Dim I As Long
Dim K As Long
Dim xCount As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
LInput:
Set xRg = Application.InputBox("please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
If xRg.Areas.Count > 1 Then
MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"
GoTo LInput
End If
On Error Resume Next
Application.ScreenUpdating = False
xCount = xRg.Columns.Count
For K = 1 To xRg.Rows.Count
xColor = xRg.Cells(K, 1).Interior.Color
For I = xCount To 1 Step -1
xRg.Cells(K, I).Interior.Color = xColor
xRg.Cells(K, I).Interior.TintAndShade = (xCount - (I - 1)) / xCount
Next
Next
End Sub

Hope it can help you, thank you!
This comment was minimized by the moderator on the site
How can I make the code to go it from right to left, Thanks in advance
This comment was minimized by the moderator on the site
Hello, Ashley,
To make the color gradient from right to left, the following vba code can help you, please try it.

Sub colorgradientmultiplecells()
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xColor As Long
Dim I As Long
Dim K As Long
Dim xCount As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
LInput:
Set xRg = Application.InputBox("please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
If xRg.Areas.Count > 1 Then
MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"
GoTo LInput
End If
On Error Resume Next
Application.ScreenUpdating = False
xCount = xRg.Columns.Count
For K = 1 To xRg.Rows.Count
xColor = xRg.Cells(K, 1).Interior.Color
For I = xCount To 1 Step -1
xRg.Cells(K, I).Interior.Color = xColor
xRg.Cells(K, I).Interior.TintAndShade = I / xCount
Next
Next
End Sub
This comment was minimized by the moderator on the site
Is it possible to perform this gradient but from bottom left to top right?
This comment was minimized by the moderator on the site
Is it possible to post a code that does gradient from top to bottom? I would really appreciate it.
This comment was minimized by the moderator on the site
I would need the code from the darkest shade at the top to the lighter shade at the bottom as well.. :(
This comment was minimized by the moderator on the site
Hi, Laura,
To sove your task, please apply the folloiwng code:

Sub colorgradientmultiplecells()

'Updateby Extendoffcie

Dim xRg As Range

Dim xTxt As String

Dim xCell As Range

Dim xColor As Long

Dim I As Long

Dim K As Long

Dim xCount As Long

On Error Resume Next

If ActiveWindow.RangeSelection.Count > 1 Then

xTxt = ActiveWindow.RangeSelection.AddressLocal

Else

xTxt = ActiveSheet.UsedRange.AddressLocal

End If

LInput:

Set xRg = Application.InputBox("please select the cells range:", "Kutools for Excel", xTxt, , , , , 8)

If xRg Is Nothing Then Exit Sub

If xRg.Areas.Count > 1 Then

MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"

GoTo LInput

End If

On Error Resume Next

Application.ScreenUpdating = False

xCount = xRg.Rows.Count

For K = 1 To xRg.Columns.Count

xColor = xRg.Cells(1, K).Interior.Color

For I = xCount To 1 Step -1

xRg.Cells(I, K).Interior.Color = xColor

xRg.Cells(I, K).Interior.TintAndShade = I / xCount

Next

Next

End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
How would I do this if I wanted it top left to bottom right gradient?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations