目标单元格更改时,电子邮件excel数据范围

时间:2013-11-21 09:35:13

标签: excel email excel-vba vba

这个宏适用于第5行,所以我需要这个宏来处理一个工作表中的所有行而不是每行的一个宏。行X和电子邮件范围A:L是所有行中的复制粘贴,即(X1 A1:L1 | X2,A2:L2 ......)

 Dim X5 As Variant

    Private Sub Worksheet_Change(ByVal Target As Range)
         If Range("X5").Value = 1 And X5 <> 1 Then

    ActiveSheet.Range("A5:L5").Select


    ActiveWorkbook.EnvelopeVisible = True


        With ActiveSheet.MailEnvelope
         .Introduction = " send thru macro "
         .Item.To = "email@gmail.com"
         .Item.Subject = "ALERT"
         .Item.Send
    End With
    End If
         X5 = Range("X5").Value

    End Sub

1 个答案:

答案 0 :(得分:1)

不确定您是否得到了答案,所以我试图回答这个问题。

为了使其适用于任何行,您可以使用Target.Row将当前单元格的行存储在变量中,然后使用它来构建范围。

另外,为了了解Worksheet_Change的工作原理,您可能希望看到THIS

这是你在尝试的吗?

Dim X5 As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    '~~> Check if the chnage happened to multiple cells
    If Target.cell.CountLarge > 1 Then Exit Sub

    Dim Rw As Long

    '~~> Get the row number of the cell that was changed
    Rw = Target.Row

    If Range("X" & Rw).Value = 1 And X5 <> 1 Then
        Application.EnableEvents = False

        Range("A" & Rw & ":L" & Rw).Select
        ActiveWorkbook.EnvelopeVisible = True

        With ActiveSheet.MailEnvelope
             .Introduction = " send thru macro "
             .Item.To = "email@gmail.com"
             .Item.Subject = "ALERT"
             .Item.Send
        End With
    End If
    X5 = Range("X" & Rw).Value

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub