2013-11-21 59 views
1

這個宏在第5行工作,所以我需要這個宏來處理一行中的所有行而不是每行一行。行X和電子郵件範圍A:L在所有線路,即複製粘貼(X1 A1:L1 | X2,A2:L2 ...)如果你有你的答案或當目標單元格發生變化時,電子郵件excel數據範圍

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 protected]" 
     .Item.Subject = "ALERT" 
     .Item.Send 
    End With 
    End If 
     X5 = Range("X5").Value 

    End Sub 
+0

如果您可以將以前的值存儲在工作表上的隱藏列中,那麼您根本不需要該變量。 –

+0

現在不要談論變量,但是謝謝傑裏解決我以前的問題,因爲你可以看到即時採取一步一步,也許你會如此善良來解決這個問題。 – user3015570

回答

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 protected]" 
      .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 
相關問題