VBA & Macro Problems

How to Fix Excel VBA UDF Returning #VALUE! with Range.DisplayFormat

John WilsonJohn Wilson Oct 29, 2026 870 views

Question details

Users need to resolve a #VALUE! error that occurs when a custom VBA function (UDF) calls Range.DisplayFormat from a worksheet cell.

Fix Excel VBA UDF Returning #VALUE! When Using Range.DisplayFormat
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user is attempting to evaluate or read the displayed formatting (like conditional formatting colors) of a cell by using a VBA user-defined function inside a worksheet cell.
Observed behavior
The cell returns a #VALUE! error because Excel restricts the use of the Range.DisplayFormat property within worksheet-called user-defined functions by design.
Before you start

Ensure you have access to the Visual Basic for Applications (VBA) editor by pressing ALT + F11, as resolving this issue requires modifying your existing VBA code.

Solution 1Recommended

Replace the UDF with a Sub Procedure

Since Excel disables Range.DisplayFormat for functions called from cells, using a standard Macro (Sub procedure) bypasses this restriction.

A Sub procedure is not bound by the strict recalculation rules of worksheet cells. Therefore, it can freely access formatting properties and write the evaluated results directly back to the target cells.

1
Open the VBA Editor

Press ALT + F11 in Excel to open the Visual Basic for Applications window.

2
Create a Sub Procedure

In your module, create a new Sub procedure (e.g., `Sub EvaluateFormat()`) instead of a Function.

3
Use Range.DisplayFormat

Write your VBA code to read the formatting. For example: `Dim cellColor As Long; cellColor = Range("A1").DisplayFormat.Interior.Color`.

4
Output the Result

Add code to write the result directly to your desired cell: `Range("B1").Value = cellColor`.

5
Run the Macro

Close the editor and run the macro from the Developer tab or assign it to a worksheet button to update your values.

Replace the UDF with a Sub Procedure
Macro Automation: You can trigger this Sub procedure using worksheet events (like Worksheet_Change) so it updates automatically when data changes, simulating the real-time update of a UDF.
Free Microsoft Office alternative

Try WPS Office for Seamless VBA Macro Management

If you are frustrated by specific execution restrictions in Microsoft Excel, WPS Office provides a lightweight, highly compatible, and robust alternative. WPS Spreadsheet comes equipped with advanced calculation capabilities and full macro support.

  1. 1. Download and Install: Visit the official WPS website, download the free version, and follow the simple installation instructions.
  2. 2. Open Your Spreadsheets: Launch WPS Spreadsheet and easily open your existing Excel workbooks and macro-enabled files.
  3. 3. Enable Macros: When prompted, simply enable macros to resume your VBA projects without losing compatibility.
Fully compatible with Microsoft Office formats, including .xlsx, .xls, and macro-enabled .xlsm files.Features a comprehensive built-in VBA editor for running and managing complex macros seamlessly.Lightweight architecture ensures faster loading times and smoother performance.Completely free to download with an intuitive, familiar interface for zero learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Range.DisplayFormat work in a Macro but not a UDF?

Microsoft designed Excel to restrict user-defined functions called directly from worksheet cells. This is to prevent them from modifying the environment or getting caught in calculation loops. Because DisplayFormat relies on the user interface state, it is disabled during the strict worksheet recalculation phase.

Can a worksheet UDF modify the formatting of another cell?

No. A user-defined function called from a cell can only return a value to the cell or cells in which it is entered. It cannot alter the formatting, color, or values of other cells in the workbook.

Is there any direct workaround to force DisplayFormat inside a UDF?

There is no direct workaround to use the DisplayFormat property within a worksheet-called UDF. You must either use a Sub procedure (macro) to process the data or recreate the conditional formatting logic mathematically within your VBA code.