Using macro to concatenate varied outputs from two columns

pjellytime

New Member
Joined
Dec 27, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

As attached, I would like to concatenate values from two columns but add all variations between the values. Would there be a way to create macro to get an output as following?
Note: the order of the output is always "Value"+"|"+"Group"

Thanks for your help!

55555.PNG
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
welcome to the board, try this it uses variant arrays which is much faster than writing directly to the workhseet.
VBA Code:
Sub test()
Dim outarr()
lasta = Cells(Rows.Count, "A").End(xlUp).Row
lastb = Cells(Rows.Count, "A").End(xlUp).Row
cola = Range(Cells(1, 1), Cells(lasta, 1))
colb = Range(Cells(1, 2), Cells(lastb, 2))
ReDim outarr(1 To (lasta - 1) * (lastb - 1), 1 To 1)
indi = 1
For i = 2 To lasta
 For j = 2 To lastb
  outarr(indi, 1) = cola(i, 1) & "|" & colb(j, 1)
  indi = indi + 1
 Next j
Next i
Range(Cells(1, 3), Cells((lasta - 1) * (lastb - 1), 3)) = outarr


End Sub
 
Upvote 0
welcome to the board, try this it uses variant arrays which is much faster than writing directly to the workhseet.
VBA Code:
Sub test()
Dim outarr()
lasta = Cells(Rows.Count, "A").End(xlUp).Row
lastb = Cells(Rows.Count, "A").End(xlUp).Row
cola = Range(Cells(1, 1), Cells(lasta, 1))
colb = Range(Cells(1, 2), Cells(lastb, 2))
ReDim outarr(1 To (lasta - 1) * (lastb - 1), 1 To 1)
indi = 1
For i = 2 To lasta
 For j = 2 To lastb
  outarr(indi, 1) = cola(i, 1) & "|" & colb(j, 1)
  indi = indi + 1
 Next j
Next i
Range(Cells(1, 3), Cells((lasta - 1) * (lastb - 1), 3)) = outarr


End Sub
Thank you so much for helping me out. The macro appears to overwrite the top row. Any suggestions on how to resolve this? Thanks again.
 

Attachments

  • Post macro.PNG
    Post macro.PNG
    10.4 KB · Views: 4
Upvote 0
change this line:
VBA Code:
Range(Cells(1, 3), Cells((lasta - 1) * (lastb - 1), 3)) = outarr
to
VBA Code:
Range(Cells(2, 3), Cells((lasta - 1) * (lastb - 1)+1, 3)) = outarr
 
Upvote 0

Forum statistics

Threads
1,215,840
Messages
6,127,214
Members
449,369
Latest member
JayHo

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