我无法将选定的单元格放入VBA中的变量中

时间:2019-02-08 20:27:36

标签: excel vba

我是VBA的新手,但我会尽力说明自己。我正在尝试从选定的单元格中创建一个变量,并使用cell.Offset精心制作邮件,但我不知道该如何前进。这是我的代码:

    Sub sendmail()
Dim i, j As Integer
Dim pagina1 As Worksheet
Set pagina1 = ActiveWorkbook.Worksheets("Example1")
Dim OutApp As Object
Dim Correo As Object
Dim cell As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
Set OutApp = GetObject("", "Outlook.Application")
Err.Clear
If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application")
OutApp.Visible = True
Set Correo = OutApp.CreateItem(0)

'THIS IS THE PLACE WHERE THE SENTENCE THAT I NEED, SHOULD BE

        email_ = cell.Value
       'subject_ = cell.Offset(0, 1).Value
        body_ = cell.Offset(0, 11).Value
        body1_ = cell.Offset(0, 6).Value
        cc_ = cell.Offset(0, 2).Value
        attach_ = cell.Offset(0, 4).Value
        destinatario_ = cell.Offset(0, 16).Value
        memofolio_ = cell.Offset(0, 17).Value
        Nmemofolio_ = cell.Offset(0, 18).Value
        Fechamemofolio_ = cell.Offset(0, 19).Value

'Crear el correo y mostrarlo
With Correo
    .To = email_
    .CC = cc_
    .Subject = "Status of the Project"
    .Body = "Infomo a usted que la iniciativa con nombre: " & body1_ & " fue enviada a " & destinatario_ & " via " & memofolio_ & " N°" & Nmemofolio_ & " con fecha " & Fechamemofolio_ & " para su revisión. Saluda Atentamente a usted, Unidad de Preinversión División de Planificación y Desarrollo"
    .Display
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With

End Sub

如果您能帮助我,我将永远感激

1 个答案:

答案 0 :(得分:2)

您需要将单元格变量设置为一个范围,同时还要指定工作表

例如

Set cell = pagina1.cells(1,1) 

Set cell = pagina1.Range("A1") 

我更喜欢上面的内容,因为它是明确且明确的。如果有选择单元格的特定操作,您也可以使用选择

Set cell = Selection