Concatenating two columns of data using a formula and the autofill command

rahulkumar88

New Member
Joined
Apr 9, 2011
Messages
16
Hi!
I need to concatenate two columns of data which are selected by the user. So the macro searches through the column headings to locate the required columns and then will concatenate them.
I'm able perform this task by running a loop through all the rows of data. The problem however is that I'm working with a significant amount of data (approx. 35000 rows) and it takes a couple of minutes for the loop to execute.
I was hoping to use a concatenation formula for the first row and then follow that up with the autofill command to work on the rest of the data. That process barely takes a few seconds when you do it manually. Unfortunately I'm unable to give the references for the required columns in the formula.
For example if I use the following formula
Range("J2").Formula = "=F2 & ""_"" & H2"
I want to know how to reference the user defined columns on the RHS.
I'd appreciate any help that you could provide.
Thanks in advance.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I wouldn't use formulas. I would build an array with the desired results and set the value of the result column at one fell swoop.
Code:
Dim firstColumn as Range
Dim secondColumn as Range
Dim resultColumn as Range, i As Long
Dim resultValues as Variant

Rem code to find firstColumn & secondColumn

Set resultColumn = Range("Z1")

resultValues = firstColumn.Values

For i = 1 to firstColumn.Rows.Count
    resultValues(i, 1) = CStr(resultValues(i, 1)) & "_" & CStr(secondColumn.Cells(i, 1))
Next i

resultColumn.Resize(firstColumn.Rows.Count, 1).Values = resultValues
 
Upvote 0
Hi Mike,
Thanks for replying. I tried running your code but was unable to do so. I added the bit where the first column and second column are determined (I assumed the columns that were picked, so that I could test the rest of the code). But I get the following error "Object doesn't support this property or method" when the code gets to this line "
resultValues = firstColumn.Values". Could you please tell me what I'm doing wrong?

Sub test()
Dim firstColumn As Range
Dim secondColumn As Range
Dim resultColumn As Range, i As Long
Dim resultValues As Variant

lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row
Range("D1:D" & lngLastRow).Select
Set firstColumn = Selection
Range("H1:H" & lngLastRow).Select
Set secondColumn = Selection
Set resultColumn = Range("Z1")

resultValues = firstColumn.Values

For i = 1 To firstColumn.Rows.Count
resultValues(i, 1) = CStr(resultValues(i, 1)) & "_" & CStr(secondColumn.Cells(i, 1))
Next i

resultColumn.Resize(firstColumn.Rows.Count, 1).Values = resultValues
End Sub
 
Upvote 0
If I run it in the immediate window I get a compile error("Invalid Use of Property"), but if I hover the cursor over firstColumn.address in the immediate window it shows "$D$1:$D$35715".
 
Upvote 0
How much data do you have. Try this version.
Code:
Sub test()
Dim firstColumn As Range
Dim secondColumn As Range
Dim resultColumn As Range, i As Long
Dim resultValues As Variant

With Range("D:D")
    Set firstColumn = Range(.Cells(1,1), .Cells(.Rows.Count,1).End(xlup))
End With
Set secondColumn = Range("H1").Resize(firstColumn.Rows.Count, 1)

Set resultColumn = Range("Z1")

resultValues = firstColumn.Values

For i = 1 To firstColumn.Rows.Count
resultValues(i, 1) = CStr(resultValues(i, 1)) & "_" & CStr(secondColumn.Cells(i, 1))
Next i

resultColumn.Resize(firstColumn.Rows.Count, 1).Values = resultValues
End Sub
 
Upvote 0
A lot of data. 35000 rows of it, which is why I'm trying to find the fastest way of carrying out the task.
I'm still getting the same error. "Object doesn't support this property or method" when the code gets to this line "resultValues = firstColumn.Values"
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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