Use a VBA Regular Expression Function for Excel Data Validation
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.

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

Validate the SKU format without VBA
Use a built-in custom validation formula when macros are unavailable or the helper-column check is unreliable.
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.

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.




