如何组合这两个workheet_change事件?

时间:2016-05-23 02:30:03

标签: excel vba excel-vba

我正在尝试运行多个工作表更改事件,但我不知道如何组合这两个宏。任何人都可以告诉我如何结合它们? 宏1

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
Else
  Application.EnableEvents = False
  newVal = Target.Value
  Application.Undo
  oldVal = Target.Value
  Target.Value = newVal
  If Target.Column = 9 Then
    If oldVal = "" Then

      Else
      If newVal = "" Then

      Else
      Target.Value = oldVal _
        & ", " & newVal

      End If
    End If
  End If
End If

exitHandler:
  Application.EnableEvents = True
End Sub

宏2

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim ws As Worksheet
Dim str As String
Dim i As Integer
Dim rngDV As Range
Dim rng As Range

If Target.Count > 1 Then Exit Sub
Set ws = Worksheets("Lists")

If Target.Row > 1 Then
  On Error Resume Next
  Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
  On Error GoTo 0
  If rngDV Is Nothing Then Exit Sub

  If Intersect(Target, rngDV) Is Nothing Then Exit Sub

  str = Target.Validation.Formula1
  str = Right(str, Len(str) - 1)
  On Error Resume Next
  Set rng = ws.Range(str)
  On Error GoTo 0
  If rng Is Nothing Then Exit Sub

  If Application.WorksheetFunction _
    .CountIf(rng, Target.Value) Then
    Exit Sub
  Else
    i = ws.Cells(Rows.Count, rng.Column).End(xlUp).Row + 1
    ws.Cells(i, rng.Column).Value = Target.Value
    rng.Sort Key1:=ws.Cells(1, rng.Column), _
      Order1:=xlAscending, Header:=xlNo, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
  End If

End If

End Sub

3 个答案:

答案 0 :(得分:0)

两个代码都合并在一个工作表更改事件下。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range, oldVal As String, newVal As String

If Target.Columns.Count > 1 Then GoTo exitHandler
If Target.Cells.Count > 1 Then GoTo exitHandler

On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Not Intersect(Target, rngDV) Is Nothing Then
    Application.EnableEvents = False

    newVal = Target.Value
    Application.Undo
    oldVal = Target.Value
    Target.Value = newVal

    If Target.Column = 9 Then
        If oldVal <> "" Then
            If newVal <> "" Then Target.Value = oldVal & ", " & newVal
        End If
    End If
End If

exitHandler:
  Application.EnableEvents = True

'Second Code Added Here...
Dim ws As Worksheet, str As String, i As Integer, rngDV As Range, rng As Range

Set ws = Worksheets("Lists")

If Target.Row > 1 Then
    On Error Resume Next
        Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo 0

    If rngDV Is Nothing Then Exit Sub
    If Intersect(Target, rngDV) Is Nothing Then Exit Sub

    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)

    On Error Resume Next
        Set rng = ws.Range(str)
    On Error GoTo 0

    If rng Is Nothing Then Exit Sub

    If Application.WorksheetFunction.CountIf(rng, Target.Value) Then
        Exit Sub
    Else
        i = ws.Cells(Rows.Count, rng.Column).End(xlUp).Row + 1
        ws.Cells(i, rng.Column).Value = Target.Value
        rng.Sort Key1:=ws.Cells(1, rng.Column), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
End If

End Sub

答案 1 :(得分:0)

谢谢大家。我跟着去了:

jayaprakash@cloudenablers:/opt/core/heatstack/heatstack$     ./dist/heatstackapp/heatstackapp 
......
......
      File "pbr/packaging.py", line 31, in <module>
      File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
        exec(bytecode, module.__dict__)
      File "setuptools/command/develop.py", line 11, in <module>
      File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
        exec(bytecode, module.__dict__)
      File "setuptools/command/easy_install.py", line 53, in <module>
      File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
        exec(bytecode, module.__dict__)
      File "setuptools/package_index.py", line 206, in <module>
      File "pkg_resources/__init__.py", line 943, in require
      File "pkg_resources/__init__.py", line 829, in resolve
    pkg_resources.DistributionNotFound: The 'setuptools' distribution was not found and is required by the application
    Failed to execute script heatstackapp

答案 2 :(得分:0)

当然你可以这样做。

Sub Macro1()
' your code here

Call Macro2
End Sub

Sub Macro2()
' your code here

End Sub