从行中的另一列获取单元格信息

时间:2014-08-08 13:24:39

标签: excel vba excel-vba

如何制作收件人姓名的电子邮件地址取自其他列。

我在列中写了名字,我想检查每个人在同一行中的日期,如果是1个月,那么就发送一封电子邮件给那个人。我只能引用一个特定的单元格,但每行都需要它,因为它向下迭代P列。

Sub Workbook_Open()

Dim Cell As Range
Dim objDate As Date

For Each Cell In Range("P3:P4").Cells

    If Cell.Value <= Date + 30 Then

        'MsgBox "Going to expire in 1 month"

        Dim appOutlook As Outlook.Application
        Dim mitOutlookMsg As Outlook.MailItem
        Dim recOutlookRecip As Outlook.Recipient

        ' Step 1: Initialize an Outlook session.
        Set appOutlook = CreateObject("Outlook.Application")
        ' Step 2: Create a new message.
        Set mitOutlookMsg = appOutlook.CreateItem(olMailItem)
        With mitOutlookMsg
            ' Step3: Add the To recipient(s) to message.
            Set recOutlookRecip = .Recipients.Add(Cells(3, 2))
            recOutlookRecip.Type = olTo
            'Set valid properties like Subject, Body, and Importance of the message.
            .Subject = "Test123"
            '.Body = "Test"
            .BodyFormat = olFormatHTML
            .HTMLBody = " TEST EMAIL "        
            .Importance = olImportanceHigh 'High importance    
            ' Resolve every Recipient's name
            For Each recOutlookRecip In .Recipients
                recOutlookRecip.Resolve
                If Not recOutlookRecip.Resolve Then
                    mitOutlookMsg.Display
                End If
            Next
            .Send
        End With
        Set mitOutlookMsg = Nothing
        Set appOutlook = Nothing       

    Else

    End If
Next Cell

End Sub

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找的是:     Range.Offset(row,col)

例如:

 For Each Cell In Range("P3:P4").Cells
    'cell.Value refers to P3:P4
    myDate = cell.Value

    'cell.Offset(0, 1).Value refers to the column one to the right of cell
    myName = cell.Offset(0, 1).Value
Next cell