简体   繁体   English

Excel在同一列中向所有人发送电子邮件,只需要向特定单元格中的所有人发送电子邮件。 怎么样?

[英]Excel sending emails to all people in one Column, need to email only person in specific cell. How?

I am creating a spreadsheet that will email supervisors 30, 15, and 7 days before the a report is due. 我正在创建一个电子表格,该电子表格将在报告到期前30、15和7天通过电子邮件发送给主管。 Everything is working, except it is sending emails to everyone in Column L and not the designated person in Column L. 一切正常,除了它正在向L列中的每个人发送电子邮件,而不是向L列中的指定人员发送电子邮件。

在此处输入图片说明

Please help. 请帮忙。 I have copied and pasted the code below. 我已经复制并粘贴了以下代码。

Public Sub GetDates()
    Dim rw As Integer
    Dim subj As String
    rw = 2

    With ActiveSheet

       Do Until .Range("A" & rw) = ""
            If .Range("M" & rw) = "" Then
                If DateAdd("D", 30, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 30, Range("L" & rw), False)
                ElseIf DateAdd("D", 15, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 15, Range("L" & rw), False)
                ElseIf DateAdd("D", 7, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 7, Range("L" & rw), False)
                End If
            End If

            If Day(Date) = 1 And .Range("G" & rw) < Date And .Range("M" & rw) = "" Then
                subj = subj & .Range("A" & rw) & ", " & .Range("B" & rw) & "--" & .Range("C" & rw) & " Report Past Due" & vbCrLf
            End If
            rw = rw + 1

        Loop

        If subj <> "" Then
            Call SendEmail(subj, "", 0, "supervisor@company.com", True)
            Call SendEmail(subj, "", 0, "aothersupervisor@company.com", True)
        End If
    End With

End Sub

Public Sub SendEmail(lName As String, fName As String, nDays As Integer, sTo As String, LastEmail As Boolean)

    Dim iMsg As Object
    Dim iConf As Object
    Dim cell As Range
    Dim Flds As Variant



    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

     iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                           = "server"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Update
        End With

For Each cell In ActiveSheet.Columns("L").Cells
        If cell.Value Like "?*@?*.?*" Then
            Set iMsg = CreateObject("CDO.Message")
    With iMsg
        Set .Configuration = iConf
        .to = cell.Value
        .CC = ""
        .BCC = ""
        .From = """Report Due"" <donotreply@company.com>"
        .Subject = "Report Due"
        .HTMLBody = lName & ", " & fName & "  <a href='http://www.website.com'>Probation Report</a> / <a href='http://www.website.com'>IDP Report</a> Due in " & nDays & " days"
        .Send
            End With
            Set iMsg = Nothing
    End If
Next cell

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

In the SendEmail subroutine you never use the Column L email parameter, sTo , and instead routine iterates through every cell in Column L: SendEmail子例程中,您永远不会使用L列电子邮件参数sTo ,而例程会遍历L列中的每个单元格:

For Each cell In ActiveSheet.Columns("L").Cells
     ...
Next cell

Remove this For loop and use sTo : 删除此For循环并使用sTo

If sTo Like "?*@?*.?*" Then
    Set iMsg = CreateObject("CDO.Message")
    With iMsg
        Set .Configuration = iConf
        .to = sTo
        .CC = ""
        .BCC = ""
        .From = """Report Due"" <donotreply@company.com>"
        .Subject = "Report Due"
        .HTMLBody = lName & ", " & fName & "  <a href='http://www.website.com'>Probation Report</a> / <a href='http://www.website.com'>IDP Report</a> Due in " & nDays & " days"
        .Send
    End With

    Set iMsg = Nothing
End If

By the way, in your GetDates subroutine, consider using Select Case over the repeated ElseIf for readability as you have multiple conditions to manage: 顺便说一句,在您的GetDates子例程中,考虑到要管理的多个条件,请考虑在重复的ElseIf使用Select Case以提高可读性:

Select Case .Range("G" & rw)

    Case DateAdd("D", 30, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 30, Range("L" & rw), False)

    Case DateAdd("D", 15, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 15, Range("L" & rw), False)

    Case DateAdd("D", 7, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 7, Range("L" & rw), False)

End Select

Alternatively, even shorter: 另外,甚至更短:

Select Case .Range("G" & rw) - Date

    Case 7, 15, 30
        Call SendEmail(Range("A" & rw), Range("B" & rw), _
                       .Range("G" & rw) - Date, Range("L" & rw), False)

End Select

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析Excel单元格。 怎么样? - Parsing excel cell. How? 在 excel 中复制一列并将每个条目/单元格加倍。 如何? - Copying a column in excel and doubling every entry/cell. How to? 在excel上分散的电子邮件列表,如何每人发送一封电子邮件? 以及如何在电子邮件正文中包含单元格的内容 - scattered email list on excel, how to send one email per person? And how to include content of a cell in email body 在 Excel 检查两个单元格中的短语,如果在两个单元格中找到任何列出的单词,则需要在另一个单元格中添加该单词。 对整列重复此操作 - In Excel Check phrases in two cell and in the two cell if any listed word found,need to add the word in another cell. repeat this for entire column 选择单元格的特定值时,使用 VBA 在 Excel 中发送电子邮件 - Sending Emails in Excel using VBA when a specific value of a cell is selected 如何忽略单元格中一组单词中的特定单词,并向一群人发送一封电子邮件? - How to ignore specific word from a group of words in a cell and send one email to group of people? 如何向列中的所有人发送一封电子邮件 - How to send a single email to all people in a column 需要在excel中创建一个宏,该宏在列中查找空白,然后从空白单元格上方填充值。 随附图片以说明 - Need to make a macro in excel that finds blanks in a column, and then fills in the value from above the blank cell. Picture included to illustrate 将数据从Word复制到Excel单元格。 然后在相邻单元格中触发公式。 怎么样? - Copy data from Word to Excel Cell. Then trigger formula in adjacent cell. How? Excel VBA在列中向不同的人发送电子邮件 - Excel VBA to Send Emails to Different People In a Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM