VBA & Macro Problems

Set an Excel or PowerPoint Chart Legend Background Color with VBA

Algirdas JasaitisAlgirdas Jasaitis Nov 13, 2026 870 views

Question details

The user needs to apply an exact RGB background color to a chart legend in Microsoft Excel or PowerPoint using VBA.

Product
Microsoft Excel and Microsoft PowerPoint
Device & OS
not provided
Scenario
A VBA macro must set the chart legend background to RGB(193, 198, 200) rather than use an indexed palette color.
Observed behavior
Legend.Interior.ColorIndex uses indexed colors and cannot specify the required custom RGB value directly.
Before you start

Select the chart you want to format and make sure VBA macros are permitted for the file. If you need to retain the macro, save the Excel workbook as .xlsm or the PowerPoint presentation as .pptm.

Solution 1Recommended

Apply an exact RGB legend background in Excel with VBA

Use the legend's fill formatting object to apply a custom RGB color to the selected Excel chart.

The key assignment is .Legend.Format.Fill.ForeColor.RGB = RGB(193, 198, 200). Unlike ColorIndex, this property accepts an RGB color value directly.

1
Select the Excel chart

In Excel, click the chart to make it the active chart. The macro below uses ActiveChart, so a worksheet cell selection alone is not sufficient.

2
Insert the VBA macro

Open Developer > Visual Basic, choose Insert > Module, and paste: Sub SetExcelLegendBackground() Dim cht As Chart Set cht = ActiveChart If cht Is Nothing Then MsgBox "Select a chart first." Exit Sub End If cht.HasLegend = True With cht.Legend.Format.Fill .Visible = msoTrue .Solid .ForeColor.RGB = RGB(193, 198, 200) .Transparency = 0 End With End Sub

3
Run the macro

Return to Excel, select the chart again, and open Developer > Macros. Select SetExcelLegendBackground and click Run. Replace the three RGB numbers in the code to use a different custom color.

Apply an exact RGB legend background in Excel with VBA
Exact color without palette indexing: The macro enables the legend and applies a visible, solid, fully opaque fill using the specified RGB value.
Free Microsoft Office alternative

Work with Excel and PowerPoint chart files in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility for a smooth move between everyday spreadsheet and presentation tasks. Use it to review and edit supported chart files; it does not fix Microsoft's VBA object model, and these macros should not be assumed to run in every WPS edition.

Open and edit Microsoft Excel .xlsx and PowerPoint .pptx files.Review chart layouts and legend appearance in supported spreadsheets and presentations.Use a familiar Office-style interface for an easier transition.Handle everyday spreadsheet and presentation work in one lightweight suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why can't Legend.Interior.ColorIndex set my exact RGB color?

ColorIndex specifies an indexed color rather than an arbitrary RGB value. Use chart.Legend.Format.Fill.ForeColor.RGB = RGB(193, 198, 200) to set the custom legend background directly.

What should I check if the legend background still looks transparent?

In the VBA editor, set .Visible = msoTrue, call .Solid, and set .Transparency = 0 inside the chart.Legend.Format.Fill block. Also confirm that the macro is targeting the intended chart.

Does this code change the legend text or the whole chart background?

No. Legend.Format.Fill controls the legend background only. To change the chart area's background, target ChartArea.Format.Fill instead; legend text formatting is separate.

Can I set the same custom color without VBA?

Yes. In Excel or PowerPoint, right-click the legend and choose Format Legend. Under Fill & Line, choose Solid fill, then open the color menu and choose More Colors. Enter the RGB values under Custom; labels may vary by Office version.

Why does the PowerPoint macro reject my chart?

The macro requires one selected editable chart shape. In Normal view, click the chart's outer border and run it again. A picture, screenshot, or non-chart object does not provide the required Chart.Legend properties.