Using VBA to Concatenate 2 Lists

Alex0013

Board Regular
Joined
Jul 23, 2014
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I'm sorry if something like this has been asked before, but I've tried searching, and I can't find the specific solution I'm looking for. What I need to do is combine 2 lists of values, but not in a "typical" way that I've seen on here for other solutions. Below is essentially what I need.

I need the Sub to take the first value in column A, and concatenate each value in column B, outputting this in column C. Then go to the next cell in column A, and concatenate each value in column to this cell, outputting this in column C as well.

I'm a beginner VBA writer, so this may be easier for some, but proving to be difficult for me :)

I'm assuming it will require at least 2 Do Until's, and some IsEmpty's maybe, but weaving it together into a masterpiece is where I'm coming up short!


PH1
PH2
VBAAnswer
E60
E001
E60E001
E94
E002
E60E002
E003
E60E003
E004
E60E004
E94E001
E94E002
E94E003
E94E004

<TBODY>
</TBODY>

Thank you in advance and please let me know if you need any additional information from me!

Alex
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the Board!

This code should do that. I tried to document it so you could follow along and see how it works:
Code:
Sub MyConcat()


    Dim myARange As Range
    Dim myBRange As Range
    Dim cellA As Range
    Dim cellB As Range
    Dim colCRow As Long
    
    Application.ScreenUpdating = False
  
'   Capture range in column A starting on row 2
    Set myARange = Range(Range("A2"), Range("A2").End(xlDown))


'   Capture range in column B starting on row 2
    Set myBRange = Range(Range("B2"), Range("B2").End(xlDown))
    
'   Set initial row to place results in for column C
    colCRow = 2
    
'   Loop through range
    For Each cellA In myARange
        For Each cellB In myBRange
'           Place concatenated value in column C
            Cells(colCRow, "C") = cellA.Text & cellB.Text
'           Increment row counter
            colCRow = colCRow + 1
        Next cellB
    Next cellA
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hey Joe,

Thank you so much! This works perfectly! Thank you very much for the comment code as well, this helps a lot in my learning of VBA.

Such a quick response too!

Alex
 
Upvote 0
Your welcome!
Glad to help!:cool:
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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