Loop Through Excel Data Validation Lists with VBA
Question details
Loop through values from multiple Excel data validation lists, skip blank entries, recalculate the model, and record the results.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A team uses an Excel model for analysis and wants to automate combinations of values from validation cells, including lists based on named ranges.
- Observed behavior
- The recorded macro works for the sample setup but cannot skip blanks and is limited when lists use named ranges.
Identify the validation input cells, their list sources, and the model output cell. Make sure you can open and edit the workbook, and use a macro-enabled .xlsm file for the test.
Use a VBA macro to test validation-list combinations
Use this approach when two validation cells control a model and you want one recorded result for each nonblank combination.
The example reads literal lists and list sources that evaluate to worksheet ranges, including named ranges. It skips empty or whitespace-only list entries and keeps numeric zero values.
The macro calculates every combination of two input lists. Adjust the sheet names and cell addresses for your model; add another nested loop for each additional input. It leaves the input cells set to the last combination tested.
Create a sheet named Results with headers in A1:C1. In this example, Model!B2 and Model!B3 are validation inputs, and Model!B10 is the output. Change those addresses in the code if your layout differs.
In desktop Excel, open Developer > Visual Basic, then select Insert > Module. If Developer is hidden, enable it under File > Options > Customize Ribbon.
Paste this code into the module: Option Explicit Sub RecordValidationResults() Dim model As Worksheet, results As Worksheet Dim list1 As Collection, list2 As Collection Dim a As Variant, b As Variant, rowNum As Long Set model = ThisWorkbook.Worksheets("Model") Set results = ThisWorkbook.Worksheets("Results") Set list1 = ValidationValues(model.Range("B2")) Set list2 = ValidationValues(model.Range("B3")) If list1.Count = 0 Or list2.Count = 0 Then MsgBox "A validation list has no nonblank values." Exit Sub End If rowNum = results.Cells(results.Rows.Count, "A").End(xlUp).Row + 1 For Each a In list1 For Each b In list2 model.Range("B2").Value = a model.Range("B3").Value = b Application.CalculateFull Do While Application.CalculationState <> xlDone DoEvents Loop results.Cells(rowNum, 1).Value = a results.Cells(rowNum, 2).Value = b results.Cells(rowNum, 3).Value = model.Range("B10").Value rowNum = rowNum + 1 Next b Next a MsgBox "Results recorded." End Sub Private Function ValidationValues(target As Range) As Collection Dim items As New Collection Dim sourceText As String, sourceRange As Range Dim cell As Range, part As Variant If target.Validation.Type <> xlValidateList Then Err.Raise vbObjectError + 1000, , target.Address & " is not a list validation cell." End If sourceText = target.Validation.Formula1 If Left$(sourceText, 1) = "=" Then On Error Resume Next Set sourceRange = target.Parent.Evaluate(Mid$(sourceText, 2)) On Error GoTo 0 If sourceRange Is Nothing Then Err.Raise vbObjectError + 1001, , "Cannot resolve list source: " & sourceText End If For Each cell In sourceRange.Cells If IsError(cell.Value2) Then Err.Raise vbObjectError + 1002, , "Error in list source: " & cell.Address End If If Len(Trim$(CStr(cell.Value2))) > 0 Then items.Add cell.Value2 Next cell Else For Each part In Split(sourceText, Application.International(xlListSeparator)) If Len(Trim$(CStr(part))) > 0 Then items.Add Trim$(CStr(part)) Next part End If Set ValidationValues = items End Function
Save the workbook as .xlsm, then use Developer > Macros to run RecordValidationResults. Check the appended rows on Results against a few manually selected combinations, including lists with blanks and named ranges.

Check SharePoint access before testing the macro
Use these checks if you cannot download, edit, or save the team's workbook.
Work with spreadsheet models in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files in WPS Spreadsheets for a straightforward move between everyday spreadsheet workflows; it does not fix Excel-specific VBA or SharePoint permission issues.

Frequently Asked Questions
How does the macro skip blank validation-list entries?
The ValidationValues function checks each source cell or literal-list item and adds it only when its trimmed text is nonempty. Numeric zero is retained.
Can the macro read a named range used by a validation list?
Yes, if the list source evaluates to a worksheet range in the input sheet's context. Check the name under Formulas > Name Manager and test it with the validation cell before running the full model.
How do I add a third validation input?
Read the third cell with ValidationValues, add another For Each loop inside the existing loops, assign its value to the input cell, and add a results column before recording the output.
Why might the recorded result be stale after calculation?
Application.CalculateFull recalculates Excel formulas, but external data refreshes or asynchronous queries may still be running. Add a completion check for the specific refresh process before reading the output cell.
Can I run this VBA macro in Excel for the web?
Excel for the web cannot run VBA macros. Open the .xlsm workbook in desktop Excel and use Developer > Macros, subject to your organization's macro security settings.




