VBA & Macro Problems

Fix Excel VBA Macro Creating Combinations with Blank Values

Nimra MalikNimra Malik Oct 10, 2026 868 views

Question details

The user needs to generate unique combinations of currencies and card types without including any blank values in the output.

How to Fix an Excel VBA Macro Generating Combinations with Blank Values
Product
Excel
Device & OS
not provided
Scenario
Generating a Cartesian product from two data lists using a VBA macro.
Observed behavior
The current VBA macro successfully generates combinations but incorrectly includes rows containing blank currency values.
Before you start

Before modifying your macro, make sure to save a backup of your current Excel workbook as a Macro-Enabled Workbook (.xlsm) to prevent any accidental data loss.

Solution 1Recommended

Use Scripting.Dictionary to Filter Blanks in VBA

This solution uses the Scripting.Dictionary object to build a unique list of non-blank values before calculating the combinations.

By leveraging dictionaries, you can automatically handle duplicates. Combined with a length check (Len and Trim), you ensure that only cells with actual text are processed into your final combinations.

1
Open the VBA Editor

Press ALT + F11 in your Excel workbook to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a new blank script window.

3
Paste the Dictionary Script

Copy and paste the following code. It uses Len(Trim(value)) > 0 to verify the cell isn't empty before adding it: Sub CreateCombinations() Dim currencies As Object, cardTypes As Object, c As Range, t As Range Dim currency, cardType, output(), i As Long Set currencies = CreateObject("Scripting.Dictionary") Set cardTypes = CreateObject("Scripting.Dictionary") currencies.CompareMode = vbTextCompare cardTypes.CompareMode = vbTextCompare With Sheets("Sheet1") For Each c In .Range("I2", .Cells(.Rows.Count, "I").End(xlUp)) If Len(Trim(c.Value)) > 0 Then currencies(c.Value) = c.Value Next c For Each t In .Range("K2", .Cells(.Rows.Count, "K").End(xlUp)) If Len(Trim(t.Value)) > 0 Then cardTypes(t.Value) = t.Value Next t End With ReDim output(1 To currencies.Count * cardTypes.Count, 1 To 2) For Each currency In currencies.Keys For Each cardType In cardTypes.Keys i = i + 1 output(i, 1) = currency output(i, 2) = cardType Next cardType Next currency With Sheets("Sheet2") .Columns("A:B").ClearContents .Range("A1:B1").Value = Array("Currency", "Card Type") If i > 0 Then .Range("A2").Resize(i, 2).Value = output End With End Sub

4
Run the Macro

Press F5 or click the Run button to execute the code. Check 'Sheet2' to see the newly generated combinations without any blanks.

Use Scripting.Dictionary to Filter Blanks in VBA
Adjust Sheet Names and Ranges: Make sure to change 'Sheet1', 'Sheet2', and the column references ('I' and 'K') in the code to match your actual workbook structure.

Seamlessly Run VBA Macros with WPS Spreadsheet

WPS Spreadsheet offers excellent compatibility with Microsoft Excel macros, allowing you to easily run VBA scripts that process data combinations, remove duplicates, and filter out blank values without changing your workflow.

  1. 1. Open Your File in WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing the data.
  2. 2. Access the Developer Tab: Navigate to the top ribbon and click on the 'Developer' tab to access macro tools.
  3. 3. Open the VBA Editor: Click 'Visual Basic' or 'VBA Editor' to open the scripting environment.
  4. 4. Run the Script: Insert the dictionary script into a module and click Run to instantly generate your valid combinations.
Highly compatible with Microsoft Excel (.xlsm) macro filesBuilt-in Developer tools to run, edit, and debug VBA scripts seamlesslyLightweight software that processes complex data operations rapidly
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro read empty cells from the column?

Using .End(xlUp) to find the last row can sometimes capture cells containing empty strings (like those returned by formulas) or previously used cells. Adding a check like If Len(Trim(c.Value)) > 0 ensures that only cells with visible text are processed.

How does Scripting.Dictionary prevent duplicate combinations?

A Dictionary in VBA requires all keys to be unique. When you assign a value to a dictionary key that already exists, it simply overwrites it rather than adding a second instance. This automatically deduplicates your list before the combinations are generated.

Can I use Power Query instead of VBA for this task?

Yes. Power Query is a powerful alternative. You can load both lists into Power Query, filter out null or blank rows, add a custom column to generate the Cartesian product, and load the clean combinations back into your worksheet without writing any VBA code.