Combine two lists into rows and columns

rainmaker1011

New Member
Joined
Jan 2, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have two lists of values and I need to combine them into one big list. See example

List A has values, List B has values. Each value from List A gets each value from List B. I need the output, where in column A I have List A values and in column B I have list B values.

Can you please suggest a nice VBA to solve this? In reality I have 300 entries in List A and 30 entries in List B :)

Thanks
Marek
1692711163989.png
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
What is the exact range where:
- List A exists
- List B exists
- you would like the results to go to?
 
Upvote 0
Thanks, I found the solution

VBA Code:
Sub ListAllCombinations()
'Updateby Extendoffice
Dim xDRg1, xDRg2, xDRg3 As Range
Dim xRg  As Range
Dim xStr As String
Dim xFN1, xFN2, xFN3 As Integer
Dim xSV1, xSV2, xSV3 As String
Set xDRg1 = Range("A2:A5")  'First column data
Set xDRg2 = Range("B2:B4")  'Second column data
Set xDRg3 = Range("C2:C4")  'Third column data
xStr = "-"   'Separator
Set xRg = Range("E2")  'Output cell
For xFN1 = 1 To xDRg1.Count
    xSV1 = xDRg1.Item(xFN1).Text
    For xFN2 = 1 To xDRg2.Count
        xSV2 = xDRg2.Item(xFN2).Text
      For xFN3 = 1 To xDRg3.Count
        xSV3 = xDRg3.Item(xFN3).Text
        xRg.Value = xSV1 & xStr & xSV2 & xStr & xSV3
        Set xRg = xRg.Offset(1, 0)
       Next
    Next
Next
End Sub
 
Upvote 0
Solution
As you have 365 you could do it with a formula
Fluff.xlsm
ABCDE
1
2Dept 1Country 1Dept 1Country 1
3Dept 2Country 2Dept 1Country 2
4Dept 3Country 3Dept 1Country 3
5Dept 4Country 4Dept 1Country 4
6Dept 5Dept 2Country 1
7Dept 6Dept 2Country 2
8Dept 2Country 3
9Dept 2Country 4
10Dept 3Country 1
11Dept 3Country 2
12Dept 3Country 3
13Dept 3Country 4
14Dept 4Country 1
15Dept 4Country 2
16Dept 4Country 3
17Dept 4Country 4
18Dept 5Country 1
19Dept 5Country 2
20Dept 5Country 3
21Dept 5Country 4
22Dept 6Country 1
23Dept 6Country 2
24Dept 6Country 3
25Dept 6Country 4
Sheet6
Cell Formulas
RangeFormula
D2:E25D2=LET(a,FILTER(A2:A500,A2:A500<>""),b,FILTER(B2:B500,B2:B500<>""),HSTACK(TOCOL(IF(SEQUENCE(,ROWS(b)),a)),TOCOL(IF(SEQUENCE(,ROWS(a)),b),,1)))
Dynamic array formulas.
 
Upvote 0
OK. Thanks for posting back. You always want to be sure to do that if you have a solution so that no one continues to work on a question that is no longer needed.

BTW, there are some slight issues with your variable declarations. It probably won't affect the output much, but could be problematic under the right circumstances (or at least may not have the desired effect).
Variable declarations lines like this:
Rich (BB code):
Dim xDRg1, xDRg2, xDRg3 As Range
do NOT do what you think they do.
Only xDRg3 will be declared as a Range. The other two will actually be variant.

If you want to restrict the variables to certain data types, each one needs to be declared explicitly, like this:
Rich (BB code):
Dim xDRg1 As Range, xDRg2 As Range, xDRg3 As Range
or this:
Rich (BB code):
Dim xDRg1 As Range
Dim xDRg2 As Range
Dim xDRg3 As Range
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top