VBA & Macro Problems

Loop Through Excel Data Validation Lists with VBA

Natalie TaylorNatalie Taylor Nov 20, 2026 868 views

Question details

Loop through values from multiple Excel data validation lists, skip blank entries, recalculate the model, and record the results.

Loop Through Excel Data Validation Lists with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Prepare the workbook

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.

2
Open the VBA editor

In desktop Excel, open Developer > Visual Basic, then select Insert > Module. If Developer is hidden, enable it under File > Options > Customize Ribbon.

3
Paste the macro

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

4
Run and verify the test

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.

Use a VBA macro to test validation-list combinations
Check source and refresh limitations: A formula-based list source must resolve to a worksheet range in the input cell's sheet context. External data refreshes or asynchronous queries may need a separate completion check; CalculateFull alone does not guarantee that those refreshes have finished.
Free Microsoft Office alternative

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.

Review spreadsheet inputs and model results in a familiar interfaceOpen and edit compatible Microsoft Excel formats, including .xlsxUse a free, lightweight Office alternativeMove everyday spreadsheet work to WPS with a familiar layout
microsoft office alternative - wps office

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.