VBA & Macro Problems

WPS Spreadsheets: Create Every Division and Product Combination with VBA

Natalie TaylorNatalie Taylor Nov 12, 2026 869 views

Question details

Create a complete table containing every combination of 103 divisions and 24 products.

Create Every Division and Product Combination in WPS Spreadsheets
Product
WPS Spreadsheets
Device & OS
not provided
Scenario
The division list and product list are stored separately, and the user needs an automatically generated Cartesian-product table with 2,472 rows.
Observed behavior
The combinations would otherwise need to be created by manually repeating division and product names.
Before you start

Place the division names in one column and the product names in another, with headers in row 1 and no blank entries inside either list. If you use the macro method, save the workbook in a macro-enabled format supported by your WPS installation.

Solution 1Recommended

Generate every combination with a VBA macro

Use this option when the division and product lists change regularly or when you want one command to rebuild the complete output table.

The macro reads the used rows in columns A and B, clears the previous output in columns D and E, and writes one row for every division-product pair. It therefore adapts when either source list grows or shrinks.

1
Open the VBA editor

In WPS Spreadsheets, open the workbook and choose Developer > Visual Basic. Select Insert > Module to create a standard module.

2
Paste the combination macro

Paste this macro into the module: Sub CreateDivisionProductCombinations()\n Dim ws As Worksheet\n Dim lastDivision As Long, lastProduct As Long\n Dim outRow As Long, i As Long, j As Long\n Set ws = ActiveSheet\n lastDivision = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row\n lastProduct = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row\n ws.Range("D:E").ClearContents\n ws.Range("D1").Value = "Division"\n ws.Range("E1").Value = "Product"\n outRow = 2\n For i = 2 To lastDivision\n For j = 2 To lastProduct\n ws.Cells(outRow, "D").Value = ws.Cells(i, "A").Value\n ws.Cells(outRow, "E").Value = ws.Cells(j, "B").Value\n outRow = outRow + 1\n Next j\n Next i\nEnd Sub

3
Run the macro

Close the Visual Basic editor, return to the worksheet, choose Developer > Macros, select CreateDivisionProductCombinations, and click Run. The generated combinations will appear in columns D and E.

4
Check the output count

For 103 divisions and 24 products, confirm that the output contains 2,472 data rows below the headers. If the lists change, run the same macro again to rebuild the table.

Expected result: The output contains each division paired with every product, producing 103 × 24 = 2,472 combinations without manual copying.
WPS Spreadsheets

Build a Complete Division-Product Combination Table in WPS Spreadsheets

WPS Spreadsheets can generate the full Cartesian product with a VBA macro or supported dynamic-array formulas. This avoids repetitive copying and makes it easier to rebuild the table when the source lists change.

  1. 1. Enter the source lists: Open WPS Spreadsheets and place divisions in column A and products in column B, using row 1 for headers.
  2. 2. Open the macro tools: Choose Developer > Visual Basic, select Insert > Module, and add the combination macro.
  3. 3. Run the generator: Return to the worksheet, choose Developer > Macros, select CreateDivisionProductCombinations, and click Run.
  4. 4. Verify the generated table: Review columns D and E and confirm that the number of data rows equals the number of divisions multiplied by the number of products.
Automatically creates every division and product pairingSupports macro-based workflows for changing source listsWorks with familiar spreadsheet formulas and table layoutsCompatible with Microsoft Excel XLSX and macro-enabled XLSM formats
microsoft office alternative - wps office

Frequently Asked Questions

How do I generate 2,472 rows from 103 divisions and 24 products?

Use the VBA macro in the recommended solution. It loops through all 103 division rows and all 24 product rows, creating 103 × 24 = 2,472 output rows.

Where should the division and product lists be placed?

Put division names in column A and product names in column B, with headers in row 1. The macro reads the used range in these columns and writes the result to columns D and E.

Can the macro handle lists that grow or shrink?

Yes. The macro finds the last nonempty row in columns A and B each time it runs. Update the source lists and run the macro again to rebuild the output.

Can I create the combinations without VBA?

Yes, if your WPS Spreadsheets version supports dynamic arrays. Enter =TOCOL(IF(SEQUENCE(,24),A2:A104)) in D2 and =TOCOL(IF(SEQUENCE(,103),B2:B25),,TRUE) in E2, then adjust the ranges and counts for different list sizes.

Why do the formula results need matching row counts?

Each division output must align with one product output in the same row. The division formula creates 24 repetitions of the division list, while the product formula creates 103 repetitions of the product list.