Vba to concatenate 3 columns

RAM1972

Board Regular
Joined
Jun 29, 2014
Messages
217
Hi All

Lookinga vba code to concatenate 3 columns irespective of data entered with results in columns columns d

Tariff No
DescriptionOrigin
Tarif No Description Origin
12345678MAZDAAE12345678 MAZDA AE
5458789FORDAE5458789 FORD AE
21411ASDDTOYOTAAE21411ASDD TOYOTA AE
DEDEDBEDFORDAEDEDED BEDFORD AE
EDEFERHUMMERATEDEFER HUMMER AT
FRRGRENAULTCNFRRG RENAULT CN
FRFRPEUGEOTCNFRFR PEUGEOT CN
RRRRCRUISER MODELCNRRRR CRUISER MODEL CN
FRRTMINI COOPER CNFRRT MINI COOPER CN

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The code below assumes you have headers in row1 and you want a space in between each value (as your example for column D suggests):
Code:
Sub Concatenate3Cols()

    Dim arr()       As Variant
    Dim x           As Long
    Const delim     As String = " "

    x = Cells(rows.count, 1).End(xlUp).row
    arr = Cells(2, 1).Resize(x - 1, 4).value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        arr(x, 4) = arr(x, 1) & delim & arr(x, 2) & delim & arr(x, 3)
    Next x
    
    Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).value = arr
    
    Erase arr
    
End Sub
 
Upvote 0
Here is another macro to consider...

Code:
Sub ConcatColumnsABC()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("D1:D" & LastRow) = Evaluate(Replace("A1:A#&"" ""&B1:B#&"" ""&C1:C#", "#", LastRow))
End Sub

While my code measures twice as fast as JackDanIce's code, you will never notice the physical difference, hence, the choice is yours... for 10,000 rows of data, my code took 0.03 seconds and JackDanIce's code took, 0.06 seconds.
 
Upvote 0
thanks workings, however can u explain a sample to me how to measure the speed a formula works.
Do agree it works very fast
 
Upvote 0
thanks workings, however can u explain a sample to me how to measure the speed a formula works.
Code:
Sub YouMacro()
  Dim AnyVariableHere
  [B][COLOR="#FF0000"]Debug.Print "?-" & Timer & "+";[/COLOR][/B]
  {Your actual code goes here}
  [B][COLOR="#FF0000"]Debug.Print Timer[/COLOR][/B]
End Sub
After you run your code, click in the line that the above code just printed and hit the Enter key... the displayed answer is in seconds... usually the number of seconds is small, so make sure to look for the exponential power of 10 at the end, for example, if this is the printout...

6.00000000049477E-02

The E-02 part means move the decimal point 2 places to the left, so the value becomes (when rounded)...

0.06
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,514
Members
449,168
Latest member
CheerfulWalker

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