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

cogumelo

Board Regular
Joined
Mar 23, 2006
Messages
175
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
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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()
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,265
Office Version
  1. 2013
Platform
  1. Windows
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
 

cogumelo

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

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

Watch MrExcel Video

Forum statistics

Threads
1,123,402
Messages
5,601,475
Members
414,452
Latest member
Dannysamworth

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
Top