Excel 仅当工作簿保存时,才基于单元格中的值发送电子邮件
我有一个单元格B8,它检查数据是否输入了当天的单元格范围,并根据计数输出一个数字。它检查空白条目,显然在填写工作表的开始,当天的所有单元格都将为空白,我希望它仅在工作表保存后执行检查 我设法让弗兰肯斯坦一起编写的代码在条件满足时立即准备一封电子邮件,我不知道如何更改它以满足我的需要Excel 仅当工作簿保存时,才基于单元格中的值发送电子邮件,excel,vba,Excel,Vba,我有一个单元格B8,它检查数据是否输入了当天的单元格范围,并根据计数输出一个数字。它检查空白条目,显然在填写工作表的开始,当天的所有单元格都将为空白,我希望它仅在工作表保存后执行检查 我设法让弗兰肯斯坦一起编写的代码在条件满足时立即准备一封电子邮件,我不知道如何更改它以满足我的需要 Sub Mail_with_outlook() Dim OutApp As Object Dim OutMail As Object Dim emlto As String, emlcc As String, eml
Sub Mail_with_outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim emlto As String, emlcc As String, emlbcc As String
Dim emlsub As String, emlbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
emlto = "email@abc.def"
emlcc = ""
emlbcc = ""
emlsub = "Raw Material Projection"
emlbody = "Good Day" & vbNewLine & vbNewLine & _
"There might be an issue with the data inputed in today's sheet"
With OutMail
.To = emlto
.CC = emlcc
.BCC = emlbcc
.Subject = emlsub
.Body = emlbody
.Display ' use .Send once tested
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
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 = 10
'range with the Formula that I want to check
Set FormulaRange = Me.Range("B8")
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_outlook
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
我会把你的逻辑放在一个事件中
如果您只检查在该范围内是否存在某些东西,而在它完全空之前,请考虑使用<代码> COUNTA /伯爵< /COD>函数。< /P> 注:
在保存工作簿之前发生 语法表达式。保存之前(保存,取消) 表达式表示工作簿对象的变量 参数 SaveAsUI:必需,布尔值,说明:如果将显示“另存为”对话框,则为True 由于所做的更改需要保存在工作簿中而显示 取消:必需,布尔值,事件发生时说明:False。如果事件 过程将此参数设置为True,此时不保存工作簿 程序完成了我忘了COUNT和COUNTA函数,这会让事情变得更简单谢谢。我用您提交的内容替换了私有子工作表_Calculate(),但保存后,我没有收到电子邮件提示。我做错什么了吗?请问电子邮件提示是什么?我的意思是带outlook的邮件功能不调用。我没有收到任何错误,但什么也没有发生。你需要留出时间来完成吗?今天晚些时候我可以试着模拟一些东西。它必须放在这个工作簿中
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'your logic goes here
End Sub