我正在编写一个代码来自动打开Outlook并根据Excel工作表上的内容创建默认电子邮件。我根据用户选择的内容选择单元格时遇到问题。示例:用户选择单元格G2 - vba代码读取D2单元格内容并放入变量。 E2,C2相同,但是在不同的变量上。这是我的代码:
Sub email_Antecip()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim email As Range
Dim data As Range
Dim valor As Range
Set email = Selection
Set valor = ActiveWorkbook.Worksheets("Planilha1").Range('this is where I need to insert the instructions')
Set data = ActiveWorkbook.Worksheets("Planilha1").Range('more instructions here')
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.to = email
.CC = ""
.BCC = ""
.Subject = "Antecipação"
.Body = "Você possui um valor de R$ " & valor & " até o dia " & data
.Attachments
.Display
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub
答案 0 :(得分:0)
这里有两个选择。
选项1 - 使用所选单元格的相对地址
选项2 - 从所选单元格中选取行并专门设置列
这样的事情:
Sub email_Antecip()
Dim email As Range
Dim data As Range
Dim valor As Range
Set email = Selection
' Option 1
Set valor = email.Offset(0, -2)
Set data = email.Offset(0, -3)
' Option 2
Set valor = ActiveWorkbook.Worksheets("Planilha1").Cells(email.Row, 4)
Set data = ActiveWorkbook.Worksheets("Planilha1").Cells(email.Row, 5)
' ...