changing the color pallet of a chart with VBA

Nils_Junker

Board Regular
Joined
Jun 2, 2023
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hi everybody,

I want to change the color pallet of my diagram which gets produced automatically.
my color pallet would be the following: (RGB(0, 0, 201), RGB(0, 149, 255), RGB(13, 189, 186), RGB(103, 187, 110), RGB(157, 115, 247), RGB(217, 87, 118), RGB(244, 156, 52), RGB(248, 223, 90), RGB(244, 221, 186), RGB(127, 127, 127), RGB(140, 80, 8), RGB(0, 0, 100))

My code for producing the chart is the following

VBA Code:
    Dim DiagrammObjekt As chartObject
    Dim DatenBereich As range
    Dim DiagrammBereich As range
    Dim colorPalette() As Variant
    
       ' Definiere den Datenbereich
    Set DatenBereich = ThisWorkbook.Sheets("Tabelle1").range("C202:BD213")

    ' Definiere den Bereich für das Diagramm
    Set DiagrammBereich = ThisWorkbook.Sheets("Tabelle1").range("A207")

    ' Erstelle das Liniendiagramm
    Set DiagrammObjekt = ThisWorkbook.Sheets("Tabelle1").ChartObjects.Add( _
        Left:=DiagrammBereich.Left, _
        Width:=500, _
        Top:=DiagrammBereich.Top, _
        Height:=300)

    ' Fülle das Diagramm mit Daten aus dem Datenbereich
    With DiagrammObjekt.chart
        .SetSourceData Source:=DatenBereich
        .ChartType = xlLine ' Ändere den Diagrammtyp hier nach Bedarf
        .HasTitle = True
        .ChartTitle.Text = "1. Diagramm"
    End With

Thanks for your help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I don't understand how you define 'color palette' so I'm not going to try give you a specific answer, only to offer a method that I often use when programming charts: Use the macro recorder to record yourself manually making whatever change to the chart it is that you want, then study the recorded macro VBA code to understand the needed syntax. Once understood, use that knowledge to modify the macro you are working on.
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top