CONCATENATE but list is too long. VBA needed?

kiefi

New Member
Joined
Nov 10, 2017
Messages
3
Hi all
I have a list of numbers in column A (list is dynamically loaded from a data base) which I need to concatenate (comma separated) into one cell, let's say B2. This could look something like:

A B
305 305,407,809
407
809

I have 2 issues:
1) because the list in column A is too long, I can't use the concatenate formula (excel simply can't handle it and gives me an error)
2) because the list is loaded from a data base, excel formulas change to #REF ! after reloading the data

I guess I need to switch to a VBA solution but so far couldn't figure out the correct code. Hope somebody can help. Thanks!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hallo,

try this code (untested)

Code:
sub iConcat()
lr = cells(rows.count,1).end(xlup).row
cells(1,2) =join(application.transpose(Range("A1:A" & lr), ", ")
end sub

regards
 
Upvote 0
I wrote this sometime ago because the native function is so unwieldy...

Code:
Function Concatenate_Range(myrange As Range, Optional myDelimiter As String)
Dim Cell As Range
Application.Volatile
For Each Cell In myrange
    If Len(Cell.Value) > 0 Then
        Concatenate_Range = Concatenate_Range & Cell & myDelimiter
    Else: Concatenate_Range = Concatenate_Range
    End If
Next Cell
If Len(myDelimiter) > 0 Then Concatenate_Range = Left(Concatenate_Range, Len(Concatenate_Range) - Len(myDelimiter))
End Function
 
Upvote 0
Hi Neil. Thanks for that. I couldn't make it work yet. Do you maybe have the excel file you use this function in? This would make it easier for me to understand how to integrate it into mine. Thank you!
 
Upvote 0
at least 1 bracket is missing

Code:
sub iConcat()
lr = cells(rows.count,1).end(xlup).row
cells(1,2) =join(application.transpose(Range("A1:A" & lr), ", "))
end sub
 
Upvote 0
In your sample data, you would enter the following into B1:
=Concatenate_Range(A1:A3,",")
 
Upvote 0
at least 1 bracket is missing

Code:
sub iConcat()
lr = cells(rows.count,1).end(xlup).row
cells(1,2) =join(application.transpose(Range("A1:A" & lr), ", "))
end sub

It should be:

Code:
cells(1,2) =join(application.transpose(Range("A1:A" & lr)), ", ")
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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