【问题标题】:Sending out an Email depending on data point using VBA使用 VBA 根据数据点发送电子邮件
【发布时间】:2014-11-24 20:04:13
【问题描述】:

如果某个数据点分别大于或小于工作表上的最大值和最小值,我基本上是在尝试发送电子邮件。问题是我设置了“如果,那么”语句,但它没有给我想要的结果。甚至出现了一些文本,我试图只显示“E”列中的数字。我想知道我哪里出错了。我将在代码中指定我认为我出错的地方。我也分别与“K26”和“K25”比较 Max 和 Min。感谢您的帮助!

Option Explicit


Public Sub OutofControl()
Dim lRow        As Long
Dim lstRow      As Long
Dim toList      As String
Dim ccList      As String
Dim bccList     As String
Dim eSubject    As String
Dim EBody       As String
Dim vbCrLf As String
Dim data As Variant
Dim ul As Variant
Dim ll As Variant



Dim ws          As Worksheet

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True


End With

Set ws = Sheets(2)
ws.Select

lstRow = WorksheetFunction.Max(3, ws.Cells(Rows.Count, "E").End(xlUp).Row)

For lRow = 1 To lstRow

data = Cells(lRow, "E").Value
ul = Range("K26")
ll = Range("K25")


    If data > ul Or data < ll And Not Null Then 
'THIS IS WHERE I THINK I NEED TO CHANGE SOMETHING SO IT DOES NOT DETECT THE TEXT



   vbCrLf = "<br><br>"

        toList = Range("B1") 'gets the recipient from col B1
        eSubject = "Text " & ActiveWorkbook.Name
        EBody = "<HTML><BODY>"
        EBody = EBody & "Dear " & Range("B1") & vbCrLf
        EBody = EBody & "Text :" & Cells(lRow, "C").Value & vbCrLf
        EBody = EBody & "Link to the SQC Chart:"
        EBody = EBody & "<A href ='Text'>Text </A>" & vbCrLf

        EBody = EBody & "</BODY></HTML>"



        MailData msgSubject:=eSubject, msgBody:=EBody, Sendto:=toList

        'Cells(lRow, "W").Value = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column W"

    End If
Next lRow


ActiveWorkbook.Save


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True

End With

End Sub



Function MailData(msgSubject As String, msgBody As String, Sendto As String, _
    Optional CCto As String, Optional BCCto As String, Optional fAttach As String)

Dim app As Object, Itm As Variant
Set app = CreateObject("Outlook.Application")
Set Itm = app.CreateItem(0)
With Itm
    .Subject = msgSubject
    .To = Sendto
    If Not IsMissing(CCto) Then .Cc = CCto
    If Len(Trim(BCCto)) > 0 Then
        .Bcc = BCCto
    End If
    .HTMLBody = msgBody
    .BodyFormat = 2 '1=Plain text, 2=HTML 3=RichText -- ISSUE: this does not keep HTML formatting -- converts all text
    'On Error Resume Next
    If Len(Trim(fAttach)) > 0 Then .Attachments.Add (fAttach) ' Must be complete path'and filename if you require an attachment to be included
    'Err.Clear
    'On Error GoTo 0
    .Save           ' This property is used when you want to saves mail to the Concept folder
    .Display      ' This property is used when you want to display before sending
    '.Send         ' This property is used if you want to send without verification
End With
Set app = Nothing
Set Itm = Nothing
End Function

【问题讨论】:

标签: excel vba numbers format


【解决方案1】:
  1. or 需要括号来与and 区分开来。 2. 下面的代码将检查数值。
For lRow = 1 To lstRow
    data = Cells(lRow, "E").Value
    ul = Range("K26")
    ll = Range("K25")
If data > ul Or data < ll Then
  If IsNumeric(data) = True And isBlank(data) = False Then

   vbCrLf = "<br><br>"
        toList = Range("B1") 'gets the recipient from col B1
        eSubject = "Text " & ActiveWorkbook.Name
        EBody = "<HTML><BODY>" _
        & "Dear " & Range("B1") & vbCrLf _
        & "Text :" & Cells(lRow, "C").Value & vbCrLf _
        & "Link to the SQC Chart:" _
        & "<A href ='Text'>Text </A>" & vbCrLf _
        & "</BODY></HTML>"

        MailData msgSubject:=eSubject, msgBody:=EBody, Sendto:=toList

        'Cells(lRow, "W").Value = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column W"

      End If
    End If
Next lRow

【讨论】:

  • 我收到不匹配错误,它突出显示“True”术语?这是为什么 ? @Chrismas007
  • 随着数据的设置方式。他们之间有空间。并且空白点正在出现。为了解决这个问题,我使用了代码:'And Not Null',但现在它根本不起作用。什么都没有发送。
  • 什么数据之间有空格?就像您在使用 data = Cells(lRow, "E").Value 定义变量数据时一样?另外,您之前使用null 来确定@​​987654326@ 的值是否包含任何内容?那么,您正在循环遍历的“E”列中的所有内容(如在哪种数据中)以及您希望在什么情况下发送您的电子邮件?
  • 我进行了更新,现在它也显示到我在“E”列中的空白处,它还显示了不符合标准的数据点。我想知道为什么这段代码不起作用?看起来它实际上会起作用,但它不起作用?
  • 尝试了一些小的语法更改...试一试。
猜你喜欢
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
相关资源
最近更新 更多