VBA & Macro Problems

Use a VBA Regular Expression Function for Excel Data Validation

Elise WilliamsElise Williams Dec 2, 2026 868 views

Question details

The user wants to use a VBA regular expression function to validate Excel entries and understand why the function is absent from the Macro dialog.

Use a VBA Regular Expression Function for Excel Data Validation
Product
Microsoft Excel
Device & OS
not provided
Scenario
Validate SKU entries in a worksheet using a public VBA function.
Observed behavior
The VBA function does not appear in the Macro dialog, which lists eligible Sub procedures rather than worksheet functions.
Before you start

Confirm that your Excel version supports VBA and that macros are enabled for the workbook. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) before adding the function.

Solution 1Recommended

Use a VBA function and helper column to check SKUs

Use this approach to test entries against a regular expression while keeping the function available to worksheet formulas.

A public Function in a standard module can be called from a worksheet formula, but it will not appear in the Macro dialog. The example below accepts two uppercase letters, a hyphen, and three digits, such as AB-123.

1
Add the VBA function

Press Alt+F11, choose Insert > Module, and paste this code: Public Function RegExpMatch(ByVal value As Variant, ByVal pattern As String) As Boolean Dim re As Object Set re = CreateObject("VBScript.RegExp") re.Pattern = pattern re.IgnoreCase = False RegExpMatch = re.Test(CStr(value)) End Function

2
Create the helper formula

In cell B2, enter =OR(A2="",RegExpMatch(A2,"^[A-Z]{2}-[0-9]{3}$")) and fill the formula down through B12.

3
Set up validation

Select A2:A12, choose Data > Data Validation, set Allow to Custom, and enter =B2 as the formula. The relative reference adjusts for each selected row.

4
Add an error message and test

On the Error Alert tab, enable the alert, select Stop, and enter a message such as “Enter an SKU in the format AB-123.” Click OK, then test valid and invalid entries in several cells.

Use a VBA function and helper column to check SKUs
Check validation behavior: A helper formula that depends on the cell being validated may not recalculate at the expected time in every Excel setup. Test both valid and invalid entries; if the result is unreliable, use the macro-free formula below. The VBA example also requires the VBScript.RegExp component to be available.
Free Microsoft Office alternative

Work with SKU spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Its Microsoft Office format support makes it easy to move existing files between applications; Excel-specific VBA validation should be tested separately.

Use familiar spreadsheet tools to review SKU lists and validation rules.Open and work with Microsoft Excel formats, including XLSX.Use a free, lightweight office suite with a familiar interface.Move existing spreadsheets into WPS Office without starting from scratch.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't my VBA function appear in Excel's Macro dialog?

The Macro dialog lists eligible Sub procedures without arguments, not Function procedures. Put a public function in a standard module through the VBA editor's Insert > Module menu to call it from worksheet formulas.

How do I allow blank SKU cells?

The example formulas use OR(A2="",...) to allow a blank entry. Apply the custom validation to A2:A12 with A2 as the relative reference for the first selected cell.

Why use anchors in the regular expression?

The ^ and $ anchors require the entire entry to match the SKU pattern. Without them, a longer string containing a matching SKU could pass the regular expression test.

What should I check if the VBA function returns an error?

Confirm that macros are enabled, the function is public and stored in a standard module, and the workbook is saved as .xlsm. The example also requires VBScript.RegExp to be available on the computer.

Will Excel data validation block pasted invalid values?

Not always. Pasting or importing data can bypass data validation, so inspect those entries separately or use an additional checking process.