VBA & Macro Problems

Excel VBA: Preserve Custom Colors in the ChooseColor Dialog

Adam DavisAdam Davis Nov 12, 2026 869 views

Question details

The user needs to retain custom colors selected in the Windows ChooseColor API dialog for later openings during the Excel session.

Product
Microsoft Excel VBA and the Windows ChooseColor API
Device & OS
not provided
Scenario
A VBA procedure opens the ChooseColor dialog and passes a custom-color array through the lpCustColors parameter.
Observed behavior
Custom colors are not available the next time the dialog opens because the array or variable loses its values when the procedure ends.
Before you start

Locate the ChooseColor API declaration and identify the array or variable supplied to lpCustColors. Confirm whether the colors only need to persist during the current Excel session or across separate launches of Excel.

Solution 1Recommended

Declare the lpCustColors array at module or global scope

Use this solution when custom colors need to remain available each time the dialog opens during the current Excel session.

A procedure-level array is recreated or goes out of scope after the procedure finishes. Declaring the array in the declarations section of a standard module keeps it in memory, allowing ChooseColor to update and reuse the custom-color slots.

This preserves the colors while Excel remains open. To retain them after Excel closes, save the array values to a file, registry entry, or worksheet and load them before opening the dialog.

1
Open the VBA editor

In Excel, press Alt+F11, then select the workbook project in the Project Explorer and choose Insert > Module to open a standard module.

2
Declare the color array at module scope

At the top of the module, outside every Sub or Function, declare the array used by lpCustColors, for example: Private mCustomColors(0 To 15) As Long.

3
Pass the persistent array to ChooseColor

In the procedure that opens the picker, pass the first element of the module-level array to the lpCustColors parameter, such as mCustomColors(0), using the argument format required by your ChooseColor API declaration.

4
Test the dialog repeatedly

Run the macro with F5, choose custom colors, close the dialog, and open it again without closing Excel. The saved custom colors should appear in the picker.

Scope controls session persistence: Keeping the lpCustColors array at module or global scope preserves custom colors for the life of the running Excel application; it does not automatically save them between Excel launches.
Free Microsoft Office alternative

Use WPS Office as a lightweight alternative for compatible spreadsheet workflows

WPS Office is a free, lightweight Microsoft Office alternative with a familiar spreadsheet interface and strong compatibility with Excel XLS and XLSX files. It can help you migrate everyday workbook tasks, although Excel-specific VBA and Windows ChooseColor API code may require redesign or a compatible macro environment.

  1. 1. Install WPS Office: Download and install WPS Office, then open WPS Spreadsheets from the application launcher.
  2. 2. Open the Excel workbook: Choose Open from the WPS Office home screen and select the XLS or XLSX workbook you want to review or migrate.
  3. 3. Check macro-dependent features: Review any Excel VBA procedures, Windows API declarations, and ChooseColor integrations separately because Office-specific macro behavior may not transfer directly.
Open and edit Excel XLS and XLSX workbooks with a familiar spreadsheet interface.Reduce dependence on Microsoft Excel for everyday spreadsheet creation and editing.Use a lightweight office suite for compatible workbook workflows and file sharing.Migrate standard spreadsheet content while reviewing Excel-specific VBA macros separately.
microsoft office alternative - wps office

Frequently Asked Questions

Why do custom ChooseColor colors disappear after the VBA procedure ends?

The lpCustColors array is likely declared inside the procedure, so it loses its values when the procedure exits. Declare the array in the declarations section of a standard module or at global scope instead.

Where should the lpCustColors array be declared in Excel VBA?

Open the VBA editor with Alt+F11, insert or select a standard module, and declare the array above all Sub and Function procedures. For example, use Private mCustomColors(0 To 15) As Long.

Will a module-level array preserve custom colors after Excel closes?

No. Module-level scope preserves the values only while the Excel application is running. To keep them between sessions, save the color values to a worksheet, file, or registry entry and reload them before calling ChooseColor.

How do I confirm that the ChooseColor dialog is using the persistent array?

Run the macro, select one or more custom colors, close the picker, and open it again during the same Excel session. If the colors remain visible, the lpCustColors parameter is using the module-level array correctly.