Aggregate numbers in a cell, the numbers to be separated by commas

Dear Experts:

I got lots of rows populated with numbers. I inserted a column at the very end where all these values are to be aggregated separated by columns. This should be done automatically.

Any idea how this could be done automatically using a formula or VBA code?

Column A     Column B      Column C     Column D      Column E       Aggregate
17                      193                  22                                                             17, 193, 22
83                      244                178               19                     23                 83, 244, 178, 19, 23
55                      343                 44                 23                                          55, 343, 44, 23

As a matter of fact I got 54 Columns.

Help is much appreciated. Thank you very much in advance.

I have attached a sample file for your convenience.

Regards, Andreas

Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try the following User Defined Function. Refer to the attached, I have placed the function in column I.
Function JoinNumbers(ByVal Rng As Range) As String
Dim str As String
Dim cell As Range
For Each cell In Rng
    If cell <> "" Then
        If str = "" Then
            str = cell.Value
            str = str & ", " & cell.Value
        End If
    End If
Next cell
JoinNumbers = str
End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
A macro you can use.

Sub Aggregate()
Dim lngLastRow As Long
Dim lngRow As Long
Dim cel As Range

lngLastRow = Range("A1048576").End(xlUp).Row

For lngRow = 1 To lngLastRow
    For Each cel In Range(Cells(lngRow, "A"), Cells(lngRow, "E"))
        If cel <> Empty Then
            Cells(lngRow, "F") = Cells(lngRow, "F") & cel.Value & ", "
        End If
    On Error Resume Next
    Cells(lngRow, "F") = Left(Cells(lngRow, "F"), Len(Cells(lngRow, "F")) - 2)
End Sub

Open in new window

Andreas HermleTeam leaderAuthor Commented:
Hi Martin,

oh, thank you very much  ... I need to catch the train, will do some testing later today and then let you know. :-O
Andreas HermleTeam leaderAuthor Commented:
Great job from both of you. I can use both.

Thank you very much for your professional and prompt support. I really appreciate it.

Regards, Andreas
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.