VBA Automatically concatenate several lines into one cell separated by a coma

cogumelo

Board Regular
Joined
Mar 23, 2006
Messages
181
Office Version
  1. 365
Platform
  1. Windows
Hi,

Hi have this list of emails on column A:

col A
line 1) email
line 2) email
line 3) email
line 4) email
etc.

i want to concatenate them all into one cell, so i can then copy it to my email client.

Something like this:

a1&","&a2&","&a3& etc.

However the last column is dynamic as it increases with more lines periodically. So i'd like to have a macro that does the concatenate by itself taking in account that the last line can be differente each time.

Is there a way to make this with VBA?


regards,

cogumelo
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Function combineEmails() As String
Application.Volatile
Dim c As Range, s As String
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    s = s & c & ","
Next
If Len(s) Then s = Left(s, Len(s) - 1)
combineEmails = s
End Function

Then in the cell you want:

=CombineEmails()
 
Upvote 0
Try This:</SPAN>
This script assumes your data is in column "A" and it places your result in Cell "E1"</SPAN>
Modify to your liking. The code highlighted in Red you can modify.</SPAN>
Code:
Sub addme()
Dim a
LastRow = Range("[B][COLOR=#b22222]A1[/COLOR][/B]").End(xlDown).Row
For a = 1 To LastRow
Range("[COLOR=#b22222][B]E1[/B][/COLOR]") = Range("[B][COLOR=#b22222]E1[/COLOR][/B]") & Cells(a, [B][COLOR=#b22222]1[/COLOR][/B]) & " "
Next
End Sub
 
Upvote 0
Hi,

Thank you both for the excellent replies. i'm going with My Answer is This's solution which does this nicely.
regards
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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