从列中获取所有非空值并转换为电子邮件列表

我试图创build一个VBA代码,发送一个电子邮件地址列E列中的电子邮件。我列在E列的电子邮件如下所示:

abc@email.com 123@email.com <Blank Cell> pop3@email.com <Blank Cell> email@test.com 

这是我的代码:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("M2")) Is Nothing And Range("M2").Value = "Send Prospect Alerts" Then Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strbody = "This is an email" On Error Resume Next With OutMail .To = Range("E6:E100").Value .CC = "" .BCC = "" .Subject = "Sales Connect Tracker: A Task has been completed" .HTMLBody = strbody 'You can add a file like this .Send 'displays outlook email 'Application.SendKeys "%s" 'presses send as a send key End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing MsgBox "Email Sent" End If End Sub 

此刻,我正在尝试在电子邮件地址字段中引用E6:E100,但这不起作用,也没有发送电子邮件。

我只能假设这是因为电子邮件地址显示如下:

  abc@email.com123@email.com<Blank Cell>pop3@email.com<Blank Cell>email@test.com 

在Outlook中,要求将电子邮件分隔如下:

 abc@email.com; 123@email.com; <Blank Cell>; pop3@email.com; <Blank Cell>; email@test.com 

也可能你会注意到,在电子邮件地址之间有一些空白单元格,我不确定这是否会被认为是一个问题或不存在,就好像没有电子邮件地址存在,那么它应该简单地忽略空白单元格。

所以我的问题基本上是如何得到这个工作,并让我的电子邮件发送到我列E中的所有电子邮件地址?

请有人告诉我我要去哪里错了,谢谢

当input一个范围或一个数组,它根本不知道该怎么做…但它会尝试设置.To到它。 而.To只接受一个string。

要得到这个:

 abc@email.com; 123@email.com; pop3@email.com; email@test.com 

尝试这个:

 Function getMailList(rng As Range) As String Dim str As String, i As Variant For Each i In rng If Len(i.Value) > 0 Then If Len(str) = 0 Then str = i Else str = str & "; " & i Next getMailList = str End Function 

并改变

 .To = Range("E6:E100").Value 

 .To = getMailList(Range("E6:E100")) 

For Each ... in ...只是返回设定范围内的每个项目
If该项目的长度不是0(可以检查isempty ,但它返回为""Then If你的输出( str )是空的Then将其设置为项目( iElse添加", "和项目它

编辑:啊哈哈…只是复制/粘贴错误…没有删除第一行中的<Blank Cell> :P