我需要对照 J 列(这是我的最低库存数量)检查 C 列中的单元格。
如果 C 列小于或等于 J 列,我需要 Excel 生成并发送电子邮件(Outlook)以提醒我们的采购部门特定商品的库存不足。
代码未触发电子邮件。
Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
'If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("J2:j2000"), Target)
If xRg Is Nothing Then Exit Sub
If xRg < Target.Value Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
您没有正确检查实际库存数量与最小值。
尝试这样的事情:
Set xRg = Intersect(Range("C2:C2000"), Target)
If xRg Is Nothing Then Exit Sub
If xRg <= xRg.Offset(0,7) Then
这仅用于比较值,未检查电子邮件的发送。