Email 使用VBA将电子邮件发送到Excel上的电子邮件列表
我写了90%的VBA代码,只需要添加以下内容。我的宏几乎在运行,如果语句和某个条件适用,它将通过电子邮件发送到某个地址。我需要它做的是运行if语句,如果它满足特定条件,则将其发送到一个包含4-5封电子邮件(可能更多)的列表,该列表位于同一工作簿中,但标题为“电子邮件列表”的不同选项卡中Email 使用VBA将电子邮件发送到Excel上的电子邮件列表,email,vba,outlook,Email,Vba,Outlook,我写了90%的VBA代码,只需要添加以下内容。我的宏几乎在运行,如果语句和某个条件适用,它将通过电子邮件发送到某个地址。我需要它做的是运行if语句,如果它满足特定条件,则将其发送到一个包含4-5封电子邮件(可能更多)的列表,该列表位于同一工作簿中,但标题为“电子邮件列表”的不同选项卡中 你可以忽略上面的部分,这是我目前正在做的 这是更新的代码。请告知,因为有8个部分,所以我将如何传输您为接下来的7个部分提供的电子邮件代码?提前谢谢你,真的很感谢你的帮助 Sub Send_Range()
你可以忽略上面的部分,这是我目前正在做的 这是更新的代码。请告知,因为有8个部分,所以我将如何传输您为接下来的7个部分提供的电子邮件代码?提前谢谢你,真的很感谢你的帮助
Sub Send_Range()
Dim row As Long
Dim col As Long
Dim rCell As Range
Dim SendTo As String
Dim i As Long
row = Sheets("Email List").UsedRange.Rows.Count
col = Sheets("Email List").UsedRange.Columns.Count
If Not IsEmpty(Range("B4")) Then
With Sheets("Email List")
For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
If rCell.Value <> "" Then
For i = 3 To row
If .Cells(i, rCell.Column).Value <> "" Then
SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
End If
Next
End If
Next
End With
End If
If IsEmpty(Range("B4")) Then
Else
ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = SendTo
.Item.Subject = "Allocations - Barclays" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
row = Sheets("Email List").UsedRange.Rows.Count
col = Sheets("Email List").UsedRange.Columns.Count
If Not IsEmpty(Range("B4")) Then
With Sheets("Email List")
For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
If rCell.Value <> "" Then
For i = 3 To row
If .Cells(i, rCell.Column).Value <> "" Then
SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
End If
Next
End If
Next
End With
End If
If IsEmpty(Range("H4")) Then
Else
ActiveSheet.Range("G3", ActiveSheet.Range("K3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - BNP" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("N4")) Then
Else
ActiveSheet.Range("M3", ActiveSheet.Range("Q3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - CITINY" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("T4")) Then
Else
ActiveSheet.Range("S3", ActiveSheet.Range("W3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - CSFB" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("Z4")) Then
Else
ActiveSheet.Range("Y3", ActiveSheet.Range("AC3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - DB" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("AF4")) Then
Else
ActiveSheet.Range("AE3", ActiveSheet.Range("AI3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - JPM" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("AL4")) Then
Else
ActiveSheet.Range("AK3", ActiveSheet.Range("AO3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - MS" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
If IsEmpty(Range("AR4")) Then
Else
ActiveSheet.Range("AQ3", ActiveSheet.Range("AU3").End(xlDown)).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = "alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End If
End Sub
子发送范围()
暗排一样长
暗色如长
变暗rCell As范围
将SendTo设置为字符串
我想我会坚持多久
行=工作表(“电子邮件列表”).UsedRange.Rows.Count
col=工作表(“电子邮件列表”).UsedRange.Columns.Count
如果不是空的(范围(“B4”)),则
带工作表(“电子邮件列表”)
对于.Range.中的每个rCell(.Cells(1,1),.Cells(1,col))
如果rCell.Value为“”,则
对于i=3到第行
如果.Cells(i,rCell.Column).Value为“”,则
SendTo=SendTo&.Cells(i,rCell.Column+1).Value&“;”
如果结束
下一个
如果结束
下一个
以
如果结束
如果为空(范围(“B4”)),则
其他的
ActiveSheet.Range(“a3”,ActiveSheet.Range(“e3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=SendTo
.Item.Subject=“分配-巴克莱”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
行=工作表(“电子邮件列表”).UsedRange.Rows.Count
col=工作表(“电子邮件列表”).UsedRange.Columns.Count
如果不是空的(范围(“B4”)),则
带工作表(“电子邮件列表”)
对于.Range.中的每个rCell(.Cells(1,1),.Cells(1,col))
如果rCell.Value为“”,则
对于i=3到第行
如果.Cells(i,rCell.Column).Value为“”,则
SendTo=SendTo&.Cells(i,rCell.Column+1).Value&“;”
如果结束
下一个
如果结束
下一个
以
如果结束
如果为空(范围(“H4”)),则
其他的
ActiveSheet.Range(“G3”,ActiveSheet.Range(“K3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-BNP”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“N4”)),则
其他的
ActiveSheet.Range(“M3”,ActiveSheet.Range(“Q3”)。结束(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-花旗”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“T4”)),则
其他的
ActiveSheet.Range(“S3”,ActiveSheet.Range(“W3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-CSFB”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“Z4”)),则
其他的
ActiveSheet.Range(“Y3”,ActiveSheet.Range(“AC3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-数据库”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“AF4”)),则
其他的
ActiveSheet.Range(“AE3”,ActiveSheet.Range(“AI3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-JPM”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“AL4”)),则
其他的
ActiveSheet.Range(“AK3”,ActiveSheet.Range(“AO3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-MS”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
如果为空(范围(“AR4”)),则
其他的
ActiveSheet.Range(“AQ3”,ActiveSheet.Range(“AU3”).End(xlDown))。选择
ActiveWorkbook.EnvelopeVisible=True
使用ActiveSheet.MailEnvelope
.Item.To=”alulla@bluemountaincapital.com" & "; alulla92@outlook.com"
.Item.Subject=“分配-”和格式(日期,“mm/dd/yyyy”)
.Item.Send
以
如果结束
端接头
通过用分号分隔地址,可以发送多封电子邮件
Email "email@example.com;email2@example.com", Subject:=:Example Email", Body:="Example Mail"
您可以在包含电子邮件的工作表中搜索需要向其发送邮件的电子邮件集,将每封电子邮件添加到一个字符串中,每个字符串之间用分号分隔
Sub Example()
Dim rCell As Range
Dim SendTo As String
Dim i As Long
For Each rCell In Range(Cells(1, 1), Cells(1, ActiveSheet.UsedRange.Columns.Count))
If rCell.Value = "DNP" Then
For i = 3 To ActiveSheet.UsedRange.Rows.Count
If Cells(i, rCell.Column).Value <> "" Then
SendTo = SendTo & Cells(i, rCell.Column + 1).Value & ";"
End If
Next
Exit For
End If
Next
Email SendTo
End Sub
子示例()
变暗rCell As范围
将SendTo设置为字符串
我想我会坚持多久
对于范围内的每个rCell(单元格(1,1),单元格(1,ActiveSheet.UsedRange.Columns.Count))
如果rCell.Value=“DNP”,则
对于ActiveSheet.UsedRange.Rows.Count的i=3
如果单元格(i,rCell.Column).Value为“”,则
SendTo=SendTo&单元格(i,rCell.Column+1).Value&“
如果结束
下一个
退出
如果结束
下一个
电子邮件发送至
端接头
您可以使用以下方式发送电子邮件:
'---------------------------------------------------------------------------------------
' Desc : Sends an email
' Ex : Email SendTo:=email@example.com, Subject:="example email", Body:="Email Body"
'---------------------------------------------------------------------------------------
Sub Email(SendTo As String, Optional CC As String, Optional BCC As String, Optional Subject As String, Optional Body As String, Optional Attachment As Variant)
Dim s As Variant 'Attachment string if array is passed
Dim Mail_Object As Variant 'Outlook application object
Dim Mail_Single As Variant 'Email object
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
'Add attachments
Select Case TypeName(Attachment)
Case "Variant()"
For Each s In Attachment
If s <> Empty Then
If FileExists(s) = True Then
Mail_Single.attachments.Add s
End If
End If
Next
Case "String"
If Attachment <> Empty Then
If FileExists(Attachment) = True Then
Mail_Single.attachments.Add Attachment
End If
End If
End Select
'Setup email
.Subject = Subject
.To = SendTo
.CC = CC
.BCC = BCC
.HTMLbody = Body
On Error GoTo SEND_FAILED
.Send
On Error GoTo 0
End With
Exit Sub
SEND_FAILED:
With Mail_Single
MsgBox "Mail to '" & .To & "' could not be sent."
.Delete
End With
Resume Next
End Sub
Function FileExists(ByVal Path As String) As Boolean
'Remove trailing backslash
If InStr(Len(Path), Path, "\") > 0 Then Path = Left(Path, Len(Path) - 1)
'Check to see if the directory exists and return true/false
If Dir(Path, vbDirectory) <> "" Then FileExists = True
End Function
'---------------------------------------------------------------------------------------
Sub Send_Range()
Dim row As Long
Dim col As Long
Dim rCell As Range
Dim SendTo As String
Dim i As Long
row = Sheets("Email List").UsedRange.Rows.Count
col = Sheets("Email List").UsedRange.Columns.Count
If Not IsEmpty(Range("B4")) Then
With Sheets("Email List")
For Each rCell In .Range(.Cells(1, 1), .Cells(1, col))
If rCell.Value <> "" Then
For i = 3 To row
If .Cells(i, rCell.Column).Value <> "" Then
SendTo = SendTo & .Cells(i, rCell.Column + 1).Value & ";"
End If
Next
End If
Next
End With
End If
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
SendTo = Left(SendTo, Len(SendTo) - 1)
.Item.To = SendTo
.Item.Subject = "Allocations - Barclays" & Format(Date, " mm/dd/yyyy")
.Item.Send
End With
End Sub