Loop to combine two columns

Sttil

New Member
Joined
Jan 7, 2015
Messages
18
Hi,

Would really appreciate your help in getting the vba code for below.

I have two columns (Key col 1 & 2) with data. Every row in "key col 1" needs to generate one entry row with two columns (doesn't really matter where, it can start output on "C2") for each value in "Key col 2", like in table below.

I've tried to make some different for next and for each statements, but my main issue I think is I suck.


Key col 1Key col 2VBA output col 1VBA output col 1
1a1a
2b1b
c1c
2a
2b
2c

Thanks and regards
Staffan
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about
VBA Code:
Sub Sttil()
   Dim Rng As Range, Cl As Range
   
   Set Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Copy Range("D2").Resize(.Rows.Count * Rng.Count)
      For Each Cl In Rng
         Cl.Copy Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(.Count)
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Sttil()
   Dim Rng As Range, Cl As Range
  
   Set Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Copy Range("D2").Resize(.Rows.Count * Rng.Count)
      For Each Cl In Rng
         Cl.Copy Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(.Count)
      Next Cl
   End With
End Sub
It worked great, thank you!!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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