【发布时间】:2022-01-14 20:21:14
【问题描述】:
我对 Excel 中的 VBA 非常陌生,只是复制了我在互联网上找到的这段代码。 问题是我希望从触发电子邮件的同一行的 A 列生成电子邮件主题文本。因此,如果 Q5 = 30,我希望从 A5 等中提取主题文本。 有谁知道如何为此编写代码?这将非常有帮助。
Dim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("Q2:Q6000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value = 30 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 2"
On Error Resume Next
With xOutMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "This should reference cell"
.Body = xMailBody
.Send 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
【问题讨论】:
标签: excel vba email cell intersect