How to Generate Combinations of Values with VBA in Excel
Question details
The user needs to generate specific combinations from a list of five values, creating subsets containing four values or two values, while identifying the remaining unselected values.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Working with a dataset where various distinct groupings (subsets) need to be extracted from a source column without manually calculating every possibility.
- Observed behavior
- Requires an automated method, such as a VBA macro or formula, to accurately compute and list all combinations, ignoring the order of the values.
Ensure the Developer tab is enabled in your spreadsheet ribbon to access the VBA editor, and remember to save your workbook as a Macro-Enabled Workbook (.xlsm) to preserve your code.
Use a VBA Macro to Generate Combinations
Using a recursive VBA script allows you to systematically generate subsets of any size (like 4 or 2) from a larger list and extract the remaining values.
When dealing with subsets, it is crucial to understand the difference between combinations and permutations. Combinations ignore the order of the items (A-B is the same as B-A), whereas permutations consider order. Since the requirement is to form sets without order mattering, a combination-based VBA function is ideal.
Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications editor.
In the VBA editor, click 'Insert' from the top menu and select 'Module' to create a blank script window.
Write or paste a recursive combination algorithm into the module. The script should be designed to loop through your specified source column and output the combinations to adjacent columns.
Press 'F5' or click the 'Run' button. The macro will process the five source values and output your subsets (e.g., sets of four or two) along with the remaining values in the target cells.

Run Macros and Generate Combinations Easily in WPS Spreadsheets
WPS Office provides a robust Spreadsheets application that fully supports VBA macros, allowing you to run combination scripts and automate complex data tasks seamlessly.
- 1. Open Your Workbook: Launch WPS Spreadsheets and open the file containing your list of values.
- 2. Access Developer Tools: Navigate to the 'Tools' tab on the ribbon and click on 'Developer' to access macro features.
- 3. Open the VBA Editor: Click on 'VBA Editor' to launch the coding environment where you can paste your combination macro.
- 4. Execute the Script: Run your macro directly from the editor to generate your four-value or two-value subsets instantly.

Frequently Asked Questions
What is the difference between combinations and permutations in VBA?
Combinations ignore the sequence of the items, meaning a set of {A, B} is considered identical to {B, A}. Permutations consider the exact order, making {A, B} and {B, A} two distinct results. You must choose the right algorithm based on whether order matters for your data.
Can I generate combinations using only spreadsheet formulas?
Yes. Modern spreadsheet tools with dynamic arrays can handle simple combinations using functions like INDEX, SEQUENCE, and FILTER. However, for dynamic subset extraction and identifying remaining values, VBA provides a more scalable and easier-to-manage solution.
Why did my VBA macro disappear after I saved and closed the file?
This happens if you save the document as a standard workbook (.xlsx). Standard workbooks automatically strip out all macro code for security reasons. To preserve your VBA scripts, you must always choose 'Save As' and select 'Excel Macro-Enabled Workbook (*.xlsm)'.




