我写了90%的VBA代码,只需添加以下内容即可。我的宏几乎运行,如果声明,如果某个条件适用,它会通过电子邮件发送到某个地址。我需要做的是运行if语句,如果它符合特定条件,则将其通过电子邮件发送到4-5封电子邮件列表(可能更多),这些电子邮件位于同一工作簿中,但名为“电子邮件列表”的不同选项卡”
你可以忽略顶部,这是我目前正在做的工作。
这是更新后的代码。请告知,因为有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
答案 0 :(得分:0)
可以通过分号分隔地址来发送多封电子邮件。
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
您可以使用以下方式发送电子邮件:
'---------------------------------------------------------------------------------------
' 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
-Edit-这将收到所有电子邮件
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