Excel'程序太大' ,如何使代码更小

时间:2014-06-14 15:40:04

标签: excel excel-vba vba

尝试各种各样的事情,只是不工作,需要更小的代码,所以我可以扩展宏。 感谢名单

Dim m2 As Variant
Dim n2 As Variant
Dim m3 As Variant
Dim n3 As Variant
Dim m4 As Variant
Dim n4 As Variant
' and so on ...
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Badentry
If Range("m2").Value = 1 And m2 <> 1 Then
ActiveSheet.Range("A2:l2").Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
  .Introduction = " send thru macro "
  .Item.To = "d@gmail.com"
  .Item.Subject = "ALERT"
  .Item.Send
 End With

End If
m2 = Range("m2").Value

If Range("n2").Value = 1 And n2 <> 1 Then
ActiveSheet.Range("A2:l2").Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
  .Introduction = " send thru macro "
  .Item.To = "@gmail.com"
  .Item.Subject = "ALERT"
  .Item.Send
End With
End If
n2 = Range("n2").Value
Badentry:
' and so on...
End sub    

1 个答案:

答案 0 :(得分:0)

我认为这会有很大帮助:

  Sub1 Range("n2"), n2
  Sub1 Range("m2"), m2
  ' and so on ..

Sub Sub1(zRange As Range, zValue As Variant)
  If zRange.Value = 1 And zValue <> 1 Then
    ActiveSheet.Range("A2:l2").Select
    ActiveWorkbook.EnvelopeVisible = True
    With ActiveSheet.MailEnvelope
      .Introduction = " send thru macro "
      .Item.To = "@gmail.com"
      .Item.Subject = "ALERT"
      .Item.Send
    End With
  End If
  zValue = zRange.Value
End Sub