Creating one text string from multiple lines

kasbac

Active Member
Joined
Jan 2, 2008
Messages
344
Hi there

I have a long list of email adresses from D4 and downwards. I need to send out an email to all these people so would very much like to combine these email adresses in to a single text string so i can copy them diretly in to my email. I neeed for each to sperated by a comma. Is it possible to write a macro that will do this for me?

Thanks in advance.

kasbac
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this - results in E1

Code:
Sub test()
Dim LR As Long
LR = Range("D" & Rows.Count).End(xlUp).Row
Range("E1").Value = Join(Application.Transpose(Range("D4:D" & LR)), ", ")
End Sub
 
Upvote 0
Here's another way to do this:
Code:
Sub eMailCombine()
lastRow = Range("D4").End(xlDown).Row
Dim eMail As String
eMail = vbNullString
For i = 4 To lastRow
    If i = 4 Then
        eMail = Cells(i, "D").Value
    Else
        eMail = eMail & ", " & Cells(i, "D").Value
    End If
Next i
Range("E1").Value = eMail
End Sub
 
Upvote 0
Try this - results in E1

Code:
Sub test()
Dim LR As Long
LR = Range("D" & Rows.Count).End(xlUp).Row
Range("E1").Value = Join(Application.Transpose(Range("D4:D" & LR)), ", ")
End Sub

Hi Vog,

This kwel vba thanks I never knew Join function in VBA.

Biz
 
Upvote 0
Vog,that is a great piece of code.

How would you be able to highlight the range instead of using Range("D" & Rows.Count).End(xlUp).Row to copy the cells into one cell?

Is there a way of using the "+" as a delimiter instead of using ","?
 
Upvote 0
Vog,that is a great piece of code.

How would you be able to highlight the range instead of using Range("D" & Rows.Count).End(xlUp).Row to copy the cells into one cell?

Is there a way of using the "+" as a delimiter instead of using ","?

Try this - it has some error checking but isn't failure-proof

Code:
Sub Concat()
Dim r1 As Range, r2 As Range
On Error Resume Next
Set r1 = Application.InputBox("Select a vertical range to concatenate", Type:=8)
If r1 Is Nothing Then Exit Sub
On Error GoTo 0
If r1.Columns.Count > 1 Then
    MsgBox "only one column allowed", vbExclamation
    Exit Sub
End If
Set r2 = Application.InputBox("Select destination cell", Type:=8)
If r2 Is Nothing Then Exit Sub
On Error GoTo 0
If r2.Cells.Count > 1 Then
    MsgBox "only one cell allowed", vbExclamation
    Exit Sub
End If
r2.Value = Join(Application.Transpose(r1), "+")
End Sub
 
Upvote 0
It works fantastic. This will save me so much manual data input. Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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