Concatenate Columns using VBA

Japhet

New Member
Joined
Aug 10, 2011
Messages
3
Hi Guys,

I need a code that will perform below requirements.

Excel VBA Concatenate Columns from A1 to AZ1 and so on.. then it will loop untill it reaches to last row e.g A10000 to B10000 and so on.. refer to below:
A1 = ABC
B1 = DEF
C1 = GHI
A2 = 123
B2 = 456
C2 = 789
result in the other sheets.
A1 = ABCDEFGHI
A2 = 123456789

Any help will be highly appreciated :)

Brgds,
Japhet
 
I have two columns which I should be able to define in the function. If I enter ConcatIf(A1:A2,B1:B2) in a single cell, the output should be

ABC - DEF
123 - 456

Please help
How do we avoid records in which either member of the pair is blank
In case it is of any use to you and you are subscribed to Office 365, then this can be done without vba using the worksheet function shown below. It is an array formula so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. You still need to format with Wrap Text.


Excel 2016 (Windows) 32 bit
BCDE
7ABC14
8DEF20
926
10GHI34
11JKL
12
13MNO26
14PQR34
15
16ABC - 14 DEF - 20 GHI - 34 MNO - 26 PQR - 34
Concat
Cell Formulas
RangeFormula
E16{=TEXTJOIN(CHAR(10),TRUE,IF((B7:B14<>"")*(C7:C14<>""),B7:B14&" - "&C7:C14,""))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The code works perfectly for me. But there is a vertical space which is seen above the output cell. I have set the vertical alignment to “top” but still the space is seen.

https://i.imgur.com/zLcpytl.jpg

Can the cell height and alignment be automatically adjusted where the =ConcatAcross function is entered
Here is my code modified to remove the Line Feed character at the top (note the only change I made is to add the code line I show in red)...
Code:
[table="width: 500"]
[tr]
	[td]Function ConcatAcross(Rng1 As Range, Rng2 As Range) As String
  Dim R As Long
  If Rng1.Rows.Count <> Rng2.Rows.Count Or Rng1.Columns.Count + Rng2.Columns.Count > 2 Then
    ConcatAcross = CVErr(xlErrRef)
  Else
    For R = 1 To Rng1.Rows.Count
      If Len(Rng1(R).Value) > 0 And Len(Rng2(R).Value) > 0 Then ConcatAcross = ConcatAcross & vbLf & Rng1(R).Value & " - " & Rng2(R).Value
    Next
    [B][COLOR="#FF0000"]ConcatAcross = Mid(ConcatAcross, 2)[/COLOR][/B]
  End If
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Yes, this is as per what I desired.
Also, thanks for the TEXTJOIN feature in Office 365. Is MS Excel becoming a friendlier word processing software?
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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