当目标单元格更改时,向电子邮件发送数据范围

这个macros在第5行工作,所以我需要这个macros在一个工作表中的所有行而不是每行的一个macros。 行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 

不知道你是否有答案,所以我试图回答这个问题。

为了使其对任何行Target.Row灵活,可以使用Target.Row将当前单元格的行存储在一个variables中,然后使用它来构造范围。

另外要了解Worksheet_Change是如何Worksheet_Change ,你可能想看看这个

这是你正在尝试?

 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