使用下一个可用行将电子邮件地址字符串拆分到不同的列中

时间:2015-11-20 22:43:44

标签: excel vba excel-vba email split

我需要一些帮助来分隔我从电子表格中提取的一串电子邮件地址。我有一个工作表设置,所以当您按下发送通知按钮时,代码会在设置列中查找一个数字,如果该数字等于12,则会从列中提取电子邮件地址4并将其插入电子邮件的“收件人”字段中。我想使用拆分功能记录所有这些电子邮件地址,但我一直只收到第一个电子邮件地址。这是我的代码:

Private Sub CommandButton1_Click()

Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Dim emailRng As Range, cl As Range
Dim sTo As String
Dim sCC As String
Dim x As Variant
Dim Y As Variant

Set emailRng = Worksheets("Sheet1").Range("D1:D500")

For Each cl In emailRng


    If cl.Offset(, 68) = 1 Then sTo = sTo & ";" & cl.Value


    If cl.Offset(, 68) = 2 Then sCC = sCC & ";" & cl.Value

Next cl

sTo = Mid(sTo, 2)
sBCC = Mid(sCC, 2)


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strto = sTo
strcc = sCC
strbcc = ""
strsub = "'NOTIFICATION'  - " & Sheet9.Cells(1, 72).Value
strbody = "<img src=Z:\Logo2.jpg width=624 height=74>" & _
          "<font size=2 font face=Verdana color=black>"


With OutMail

    .SentOnBehalfOfName = ""
    .to = strto
    .cc = strcc
    .bcc = strbcc
    .Subject = strsub
    .importance = 2
    'You can add a file to the mail like this
    .HTMLBody = strbody
    .Display    ' or use .Send

End With

Set OutMail = Nothing
Set OutApp = Nothing
x = Split(sTo, "")


 Y = Split(sCC, "")

     lrtag = Sheets("Log Sheet").Range("B" & Rows.Count).End(xlUp).Row
     Sheets("Log Sheet").Cells(lrtag + 1, "B").Value = "NOTIFICATION SENT"
      Sheets("Log Sheet").Cells(lrtag + 1, "C").Value = "DATE SENT"
      Sheets("Log Sheet").Cells(lrtag + 1, "D").Value = Now
      Sheets("Log Sheet").Cells(lrtag + 1, "E").Value = "NOTIFICATION SENT TO:"
      Sheets("Log Sheet").Cells(lrtag + 1, "F").Value =Application.Transpose(x)

 Sheets("HEADER").Select

 Unload Me

 End Sub

我想我需要扩展声明:

Sheets("Log Sheet").Cells(lrtag + 1, "F").Value =Application.Transpose(x)

1 个答案:

答案 0 :(得分:2)

如果要将一维数组放在一行中,则不需要使用Transpose,但是您需要指定目标的完整范围:

Sheets("Log Sheet").Cells(lrtag + 1, "F").Resize(1,ubound(x)+1).Value = x

如果您希望数组垂直移动,则需要使用Transpose。