【问题标题】:Generate Email Subject based of a varying cell values根据不同的单元格值生成电子邮件主题
【发布时间】: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


    【解决方案1】:

    您可以使用Target.Row获取更改的单元格的行,使用"A" & Target.Row获取同一行的A列中单元格的地址,并将其作为参数传递给Mail_small_Text_Outlook Sub:

    Call Mail_small_Text_Outlook(Range("A" & Target.Row).Value)
    

    然后您需要更改 Sub 定义以获取参数并在创建电子邮件时将其用于主题:

    Sub Mail_small_Text_Outlook(mailSubject As String)
    
        ...
    
            .Subject = mailSubject
    
        ...
    

    【讨论】:

    • 这太棒了,花了我一些修修补补,但我明白了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多