【问题标题】:Sending an email for values that exceed a limit为超出限制的值发送电子邮件
【发布时间】:2018-02-26 17:30:08
【问题描述】:

当我尝试执行以下代码时,调试器突出显示 strbody 行,该行决定哪些单元格将输出到电子邮件。

我不知道如何激活 FormulaCell,以便当某一列中超过某个值时,该行将能够用于在电子邮件中输入信息。

Option Explicit
Public FormulaCell As Range

    Private Sub Worksheet_Calculate()
    Dim FormulaRange As Range
    Dim NotSentMsg As String
    Dim MyMsg As String
    Dim SentMsg As String
    Dim MyLimit As Double

    NotSentMsg = "Not Sent"
    SentMsg = "Sent"

    'Above the MyLimit value it will run the macro
    MyLimit = 1

    'Set the range with Formulas that you want to check
    Set FormulaRange = Me.Range("B3:B197")

    On Error GoTo EndMacro:
    For Each FormulaCell In FormulaRange.Cells
        With FormulaCell
            If IsNumeric(.Value) = False Then
                MyMsg = "Not numeric"
            Else
                If .Value = MyLimit Then
                    MyMsg = SentMsg
                    If .Offset(0, 1).Value = NotSentMsg Then
                        Call Mail_with_outlook2
                    End If
                Else
                    MyMsg = NotSentMsg
                End If
            End If
            Application.EnableEvents = False
            .Offset(0, 1).Value = MyMsg
            Application.EnableEvents = True
        End With
    Next FormulaCell

    ExitMacro:
    Exit Sub

    EndMacro:
    Application.EnableEvents = True

    MsgBox "Some Error occurred." _
         & vbLf & Err.Number _
         & vbLf & Err.Description

    End Sub

Option Explicit

Public FormulaCell As Range

Sub Mail_with_outlook1()
'For mail code examples visit my mail page at:
'http://www.rondebruin.nl/sendmail.htm
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

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

strto = "ron@something.abc"
strcc = ""
strbcc = ""
strsub = "Customers"
strbody = "Hi Ron" & vbNewLine & vbNewLine & _
          "The total Customers of all stores this week is : " & Cells(FormulaCell.row, "B").Value & _
          vbNewLine & vbNewLine & "Good job"

With OutMail
    .To = strto
    .CC = strcc
    .BCC = strbcc
    .Subject = strsub
    .Body = strbody
    'You can add a file to the mail like this
    '.Attachments.Add ("C:\test.txt")
    .Display    ' or use .Send
End With

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Sub Mail_with_outlook2()
'For mail code examples visit my mail page at:
'http://www.rondebruin.nl/sendmail.htm
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

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

strto = "MAIL@EMAIL.CO.UK"
strcc = ""
strbcc = ""
strsub = "S888 OVERDUE"
strbody = "THIS S888 IS NOW OVERDUE" & Cells(FormulaCell.row, "A").Value & vbNewLine & vbNewLine & _
          "Your total of this week is : " & Cells(FormulaCell.row, "K:Q").Value & _
          vbNewLine & vbNewLine & "HURRY UP!!"

With OutMail
    .To = strto
    .CC = strcc
    .BCC = strbcc
    .Subject = strsub
    .Body = strbody
    'You can add a file to the mail like this
    '.Attachments.Add ("C:\test.txt")
    .Display    ' or use .Send
End With

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

【问题讨论】:

    标签: excel vba email basic


    【解决方案1】:

    正如 Vityata 所说,Cells(FormulaCell.row, "K:Q").Value 表达式无效

    如果您需要引用与FormulaCell Range 位于同一行的 K 到 Q 列中的单元格,请使用

    Worksheetfunction.Sum(Intersect(FormulaCell.EntireRow, Range("K:Q")))
    

    因此,如果您想返回这些单元格的总和,请使用

    strbody = "THIS S888 IS NOW OVERDUE" & Cells(FormulaCell.row, "A").Value & vbNewLine & vbNewLine & _
          "Your total of this week is : " & Worksheetfunction.Sum(Intersect(FormulaCell.EntireRow, Range("K:Q"))) & _
          vbNewLine & vbNewLine & "HURRY UP!!"
    

    但是我在您的代码中看到了一个隐藏错误,您在 Worksheet_Calculate() 事件处理程序和“MACRO”模块中都使用了 Public FormulaCell As Range

    通过这样做,您将实例化两个不同的 Range 变量,一个附加到工作表事件处理程序,一个附加到 MACRO 模块,并且没有明确地将它们限定为它们的 Parent 对象您将在当前“活动”的 VBA 模块中引用 Range 变量。

    因此,在执行Call Mail_with_outlook2 语句之前的代码中,任何FormulaCell 都将引用For Each FormulaCell In FormulaRange.Cells 循环的当前迭代器Range 变量。但是一旦程序执行进入Mail_with_outlook2(),“驻留”变量FormulaCell仍然需要设置,所以它是Nothing

    长话短说你有两个选择:

    • 去掉 MACRO 模块中的 Public FormulaCell As Range 并明确限定所有 FormulaCell 引用直到 Sheet 对象所在的 Worksheet_Calculate()

      例如,假设“Sheet1”是后者的名称,那么你会写:

      strbody = "Hi Ron" & vbNewLine & vbNewLine & _
                "The total Customers of all stores this week is : " & Cells(Worksheets("Sheet1").FormulaCell.Row, "B").value & _
        vbNewLine & vbNewLine & "Good job"
      
    • 去掉 Worksheet 和 MACRO 模块中的所有 Public FormulaCell As Range 并让 Mail_with_outlook1() sub 接受“FormulaCell”Range 参数

      所以在Worksheet_Calculate(),而不是:

      Call Mail_with_outlook2
      

      你会:

      Mail_with_outlook2 FormulaCell
      

      Mail_with_outlook1() 中,而不是:

      Sub Mail_with_outlook1()
      

      你应该有

      Sub Mail_with_outlook1(FormulaCell As Range)
      

    【讨论】:

    • @RyanLigget,有什么反馈吗?
    【解决方案2】:

    错误在这里:

    Cells(FormulaCell.row, "K:Q").Value根据你的需要,试试这样:

    Cells(FormulaCell.Row, "K").Value
    Range("K" & FormulaCell.Row & ":Q" & FormulaCell.Row).Values
    

    或者别的什么。

    一般来说,每当您认为错误出现在您知道的某个地方时,请尝试使代码尽可能小,并查看它是否运行。在您的情况下,这是一件小事:

    Public Sub TestMe()
    
        Dim strBody As String
    
        For Each FormulaCell In Range("A1:A2")
            strBody = "Hi Ron" & vbNewLine & vbNewLine & _
            "The total Customers of all stores this week is : " _
            & Cells(FormulaCell.Row, "B").Value & _
            vbNewLine & vbNewLine & "Good job"
            Debug.Print strBody
        Next FormulaCell
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2016-12-03
      • 2017-09-11
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多