Excel宏,组合两个Private Sub worksheet_change

时间:2014-08-15 11:59:49

标签: excel excel-vba vba

我之前在这个网站上搜索过,但我的代码并没有真正找到相同的情况。希望有人可以帮助我。 如何组合下面的两个Private sub?

  

第一个代码

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("P:P")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
Dim NR As Long
With Application
  .EnableEvents = False
  .ScreenUpdating = False
  Select Case Target.Value
    Case "CLOSED"
      NR = Worksheets("Closed").Range("D1500").End(xlUp).Offset(1).Row
      Range("B" & Target.Row & ":P" & Target.Row).Copy Worksheets("Closed").Range("B" & NR)
      Rows(Target.Row).Delete
    Case "Re-handover"
      NR = Worksheets("Handover").Range("D1500").End(xlUp).Offset(1).Row
      Range("E" & Target.Row & ":O" & Target.Row).Copy Worksheets("Handover").Range("E" & NR)
      ' Rows(Target.Row).Delete
      End Select
  .EnableEvents = True
  .ScreenUpdating = True
End With
End Sub
  

第二个代码

Option Explicit
Public preValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Column <> 15 Then Exit Sub
    Target.ClearComments
Target.AddComment.Text Text:="Updated " & Format(Date, "dd mmm yyyy") & " " & Format(Time, "hh:mm") & Chr(10) & "By " & Environ("UserName")
End Sub

之前非常感谢你

1 个答案:

答案 0 :(得分:0)

这会有用吗?

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub

*If target.column=15 then
    Target.ClearComments
    Target.AddComment.Text Text:="Updated " & Format(Date, "dd mmm yyyy") & " " & Format(Time, "hh:mm") & Chr(10) & "By " & Environ("UserName")
else
endif *


If Intersect(Target, Range("P:P")) Is Nothing Then Exit Sub
If Target = "" Then Exit Sub
Dim NR As Long
With Application
  .EnableEvents = False
  .ScreenUpdating = False
  Select Case Target.Value
    Case "CLOSED"
      NR = Worksheets("Closed").Range("D1500").End(xlUp).Offset(1).Row
      Range("B" & Target.Row & ":P" & Target.Row).Copy Worksheets("Closed").Range("B" & NR)
      Rows(Target.Row).Delete
    Case "Re-handover"
      NR = Worksheets("Handover").Range("D1500").End(xlUp).Offset(1).Row
      Range("E" & Target.Row & ":O" & Target.Row).Copy Worksheets("Handover").Range("E" & NR)
      ' Rows(Target.Row).Delete
      End Select
  .EnableEvents = True
  .ScreenUpdating = True
End With
End Sub