VBA: Look at column A and column B, Paste all combinations of column A and B in columns C and D

PorcupineGuy

New Member
Joined
Nov 10, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello all,

I took a quick peek around the forum and found something similar to what I want, but not quite: VBA: Look at column A, Paste all combination pairs into B & C

What I am looking for is:
ABCD
Org1ID1Org1ID1
Org2ID2Org1ID2
Org3ID3Org1ID3
ID4Org1ID4
ID5Org1ID5
Org2ID1
Org2ID2
Org2ID3
Org2ID4
Org2ID5
Org3ID1
Org3ID2
Org3ID3
Org3ID4
Org3ID5

The output in columns C and D will have the numbers rows = [number of rows in A]*[number of rows in B] -- so for the example above, since column A has 3 rows and column B has 5 rows, columns C and D will be 15 rows in total.

Thanks in advance!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
VBA Code:
Sub myFunction()
  Dim lRowA As Integer, lRowB As Integer, r As Integer
  lRowA = Cells(Rows.Count, 1).End(xlUp).Row
  lRowB = Cells(Rows.Count, 2).End(xlUp).Row
  r = 1
  For i  = 1 To lRowA
    For j = 1 To lRowB
      Cells(r, 3).Value = Cells(i, 1).Value
      Cells(r, 4).Value = Cells(j, 2).Value
      r = r + 1
    Next j
  Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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