Generate Every 4-Country Combination and Folder in Excel with VBA
Question details
The user needs to generate every possible four-country combination from a nine-country list and create a separate folder named for each combination.

- Product
- WPS Office Spreadsheets
- Device & OS
- not provided
- Scenario
- A user has a nine-country dataset and wants to automate creating one folder per four-country combination.
- Observed behavior
- Manual combination and folder creation is impractical, so a VBA macro is required to enumerate 126 combinations and create the folders.
Make sure the country list is in a single column with no blank rows, and enable the Developer tab in WPS Office Spreadsheets or Excel before opening the VBA editor.
Generate All Four-Country Combinations and Create Folders with a VBA Macro
Use this solution when you want to automate the full process: enumerate the 126 combinations from nine countries and create a named folder for each combination.
This macro reads country names from cells A1:A9, loops through every unique four-country combination, and creates a folder for each combination under the workbook path.
Folder names use a comma and space separator. Invalid Windows folder characters are replaced with underscores so the macro can run without naming errors.
In WPS Office Spreadsheets or Excel, click the Developer tab and select Visual Basic. If the Developer tab is not visible, enable it from File > Options > Customize Ribbon > Developer.
In the Visual Basic Editor, click Insert > Module to create a module where the macro will be placed.
Paste the following macro, then press F5 or click Run > Run Sub/UserForm: Sub CreateCountryCombinationFolders() Dim countries(1 To 9) As String Dim i As Long, j As Long, k As Long, l As Long Dim basePath As String, folderName As String, count As Long basePath = ThisWorkbook.Path & Application.PathSeparator For i = 1 To 9 countries(i) = ActiveSheet.Cells(i, 1).Value Next i For i = 1 To 6 For j = i + 1 To 7 For k = j + 1 To 8 For l = k + 1 To 9 folderName = countries(i) & ", " & countries(j) & ", " & countries(k) & ", " & countries(l) folderName = Replace(folderName, Chr(47), "_") folderName = Replace(folderName, Chr(92), "_") folderName = Replace(folderName, Chr(58), "_") folderName = Replace(folderName, Chr(42), "_") folderName = Replace(folderName, Chr(63), "_") folderName = Replace(folderName, Chr(34), "_") folderName = Replace(folderName, Chr(60), "_") folderName = Replace(folderName, Chr(62), "_") folderName = Replace(folderName, Chr(124), "_") If Len(Dir(basePath & folderName, vbDirectory)) = 0 Then MkDir basePath & folderName count = count + 1 Next l Next k Next j Next i MsgBox count & " folders created." End Sub
After the macro completes, open the workbook folder. You will see 126 folders, each named with a four-country combination.

Verify the Combination Count with the COMBIN Function
Use this to quickly confirm how many folders the macro should create before or after running it.
Automate Combination Folder Tasks in WPS Office Spreadsheets
WPS Office Spreadsheets supports VBA-style macros and standard Excel functions, so you can generate all four-country combinations, create the 126 folders, and keep working in a familiar interface.
- 1. Prepare the country list: Open WPS Office Spreadsheets and enter the nine countries in cells A1:A9, one per row.
- 2. Enable the Developer tab: Click File > Options > Customize Ribbon, check Developer, and click OK.
- 3. Open the VBA editor and insert a module: Click Developer > Visual Basic, then Insert > Module.
- 4. Run the folder-generation macro: Paste the VBA code, set basePath to your desired folder, and press F5 to run it.
- 5. Verify the output: Open the target folder and confirm that 126 combination folders were created.

Frequently Asked Questions
How many four-country combinations can be made from nine countries?
Use =COMBIN(9,4) in WPS Office Spreadsheets or Excel. The result is 126 unique combinations.
Does the macro work if my country names contain spaces or commas?
Spaces and commas are valid in Windows folder names. The macro can replace characters that are not allowed in folder names, such as slash, backslash, colon, asterisk, question mark, double quote, angle brackets, and pipe, with underscores.
Can I run this macro in WPS Office Spreadsheets?
Yes. On Windows, WPS Office Spreadsheets supports VBA-compatible macros. Open the VBA editor from Developer > Visual Basic, insert a module, paste the code, and run it.
Why do I get a path not found error when the macro runs?
The base path in the macro does not exist. Set basePath to an existing folder path, or use ThisWorkbook.Path and make sure the workbook has been saved once.




