使用VBA

时间:2019-07-15 16:49:55

标签: excel vba

我正在开发一种自动通知系统,该系统将在特定单元格中填充个人首字母后立即使用VBA发送电子邮件。这标志着团队的其他成员已经创建了另一个任务。

这是我当前正在使用的代码,但是我无法弄清楚如何从活动单元格中找出三个单元格。

Sub Mail_small_Text_Outlook()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim workrng As Range
Dim RNG As Range
Dim xOffsetColumn As Integer


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)



Application.DisplayAlerts = True


strbody = "" & vbNewLine & vbNewLine & _
          "Please review the QPC" & vbNewLine & _
          "" & vbNewLine & _
          "" & vbNewLine & _
          ""

On Error Resume Next
Application.DisplayAlerts = False
With OutMail
    .To = "rschmucker@kessington.com;"
    .CC = ""
    .BCC = ""
    .Subject = "QPC"
    .Body = "New entry on the QPC"
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    .display   'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

如果H4单元格填充了正确的初始字母,那么我需要身体包括A4,B4,C4单元格。

当填充H5时,A5,B5,C5也是同样的情况,依此类推。

1 个答案:

答案 0 :(得分:0)

要回答无法找到ActiveCell的相应单元格的特定问题,可以使用OFFSET property来引用它们,或使用Activecell {{1 }}属性。

Row

请注意,上面的某些代码是故意进行说明的。您可以将'This will reference one cell to the left of your activecell MsgBox ActiveCell.Offset(0, -1).Address & " is one cell to left" 'This will reference the cell column A on the same row of your ActiveCell MsgBox ActiveCell.Worksheet.Cells(ActiveCell.Row, Range("A:A").Column).Address & " is the cell in the A column" 'This is how you would include Column a and b in your email separated by a return line. .body = ActiveCell.Worksheet.Cells(ActiveCell.Row, Range("A:A").Column) & Chr(10) _ & ActiveCell.Worksheet.Cells(ActiveCell.Row, Range("B:B").Column) 替换为Range("A:A").Column,也可以不调用Worksheet属性。