VBA & Macro Problems

Use Cell Values in a Dynamic Excel Data Validation Error Message

Natalie TaylorNatalie Taylor Nov 25, 2026 871 views

Question details

Display the permitted range in an Excel data validation error message using values stored in other cells.

Use Cell Values in a Dynamic Excel Data Validation Error Message
Product
Microsoft Excel
Device & OS
not provided
Scenario
A discount entered in A2 must fall between a minimum in C2 and a maximum in D2, and the error message should reflect those limits.
Observed behavior
The validation rule should use the current limits, and the error message should display those same values.
Before you start

Open the workbook in desktop Excel and identify the input cell and its minimum and maximum cells. This example uses A2, C2, and D2 and formats the limits as percentages.

Solution 1Recommended

Update the validation error message with worksheet VBA

Use a custom validation formula for the range and worksheet code to refresh the message when either limit is edited.

Excel's validation formula can reference other cells, but the Error Alert message does not automatically evaluate cell references. The VBA code below updates the stored message when C2 or D2 is edited.

1
Enter the range limits

Enter the minimum discount in C2 and the maximum discount in D2, such as 10% and 20%.

2
Create the validation rule

Select A2, then go to Data > Data Validation. Under Settings, choose Custom and enter =AND(A2>=$C$2,A2<=$D$2) in the Formula box.

3
Enable the error alert

In the Data Validation dialog, open Error Alert, select Show error alert after invalid data is entered, choose Stop, and enter a temporary message. Click OK.

4
Add the worksheet code

Right-click the worksheet tab, select View Code, and paste this code into that worksheet's code window: Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Me.Range("C2:D2")) Is Nothing Then Exit Sub Me.Range("A2").Validation.ErrorMessage = _ "The discount range is from " & Format(Me.Range("C2").Value, "0%") & _ " to " & Format(Me.Range("D2").Value, "0%") & "." End Sub

5
Save and test

Use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros when prompted, re-enter a value in C2 or D2 to initialize the message, then enter an out-of-range value in A2 to test it.

Update the validation error message with worksheet VBA
When the message refreshes: This code responds to edits in C2 or D2, not changes caused only by formula recalculation. It requires macros to be enabled in desktop Excel; Excel for the web does not run worksheet VBA.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and compatibility with common Excel files for an easier move between office suites. It does not fix Excel's VBA error-message behavior.

Use spreadsheet data validation tools to help control cell entries.Open and edit common Microsoft Excel formats, including XLSX and XLS.Navigate spreadsheets in a familiar, easy-to-use interface.Work with a lightweight office suite at no cost.
microsoft office alternative - wps office

Frequently Asked Questions

Can an Excel data validation error message contain a cell formula?

The Error Alert message does not automatically evaluate a formula or cell reference. You can use worksheet VBA to update the stored message when the referenced cells change.

Will the validation rule still use the current limits if macros are disabled?

The custom formula in Data > Data Validation can still use the current values in C2 and D2, but the VBA-generated error message will not refresh while macros are disabled.

Why does the message not update when a limit changes through a formula?

Worksheet_Change responds to cell edits, not formula recalculation alone. If C2 or D2 contains a formula, the message needs a different refresh approach, such as code that handles worksheet recalculation.

Does data validation stop every invalid value from reaching A2?

A Stop alert blocks an invalid value typed into the validated cell, but data validation can be bypassed by actions such as pasting values. Check existing or pasted data separately.