如何解决有关代码优先级的Excel VBA问题?

时间:2017-11-22 16:04:13

标签: excel vba excel-vba

我的某个工作表上有一个激活此代码的停用事件:

Dim Total_rows_DTR As Long
Dim Total_rows_PU As Long
Dim Total_rows_PR As Long
Dim Total_rows_PTS As Long
Dim Total_Columns4 As Long
Dim Total_rows_EX4 As Long
Dim Sum As Long
Dim j As Long
Dim w As Long
Dim h As Long
Dim e As Long
Dim q As Long

Total_rows_DTR = Workbooks("Revised-Payroll (VBA Copy).xlsm").Worksheets("DTR").Range("B" & Rows.Count).End(xlUp).Row
Total_rows_PU = Workbooks("Revised-Payroll (VBA Copy).xlsm").Worksheets("Payroll Update").Range("A" & Rows.Count).End(xlUp).Row
Total_rows_PR = Workbooks("Revised-Payroll (VBA Copy).xlsm").Worksheets("Payroll - Regular").Range("A" & Rows.Count).End(xlUp).Row

For j = 2 To Total_rows_PU
    If Application.WorksheetFunction.Index(Worksheets("Payroll Tables and Settings").Range("D2:D1048576"), Application.WorksheetFunction.Match(Worksheets("Payroll Update").Cells(j, 1), Worksheets("Payroll Tables and Settings").Range("A2:A1048576"), 0)) <> Worksheets("Payroll Update").Cells(j, 5) _
    And IsEmpty(Worksheets("Payroll Update").Cells(j, 5)) = False Then
        If Application.WorksheetFunction.Index(Worksheets("Payroll Tables and Settings").Range("D2:D1048576"), Application.WorksheetFunction.Match(Worksheets("Payroll Update").Cells(j, 1), Worksheets("Payroll Tables and Settings").Range("A2:A1048576"), 0)) = "Extra" Then
            For h = 3 To Total_Columns4
                If Worksheets("Payroll - Extra").Cells(1, h) = Worksheets("Payroll Update").Cells(j, 1) Then
                    For q = 2 To Total_rows_EX4
                        If Worksheets("Payroll - Extra").Cells(q, 2) < Worksheets("Payroll Update").Cells(j, 2) And IsFormula(Worksheets("Payroll - Extra").Cells(q, h)) = True Then
                            Worksheets("Payroll - Extra").Cells(q, h).Copy
                            Worksheets("Payroll - Extra").Cells(q, h).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                            :=False, Transpose:=False
                        End If
                        If Worksheets("Payroll - Extra").Cells(q, 2) >= Worksheets("Payroll Update").Cells(j, 2) And Worksheets("Payroll - Extra").Cells(q, 1) >= Worksheets("Payroll Update").Cells(j, 2) Then
                            Worksheets("Payroll - Extra").Cells(q, h).Clear
                        ElseIf Worksheets("Payroll - Extra").Cells(q, 2) >= Worksheets("Payroll Update").Cells(j, 2) And Worksheets("Payroll - Extra").Cells(q, 1) < Worksheets("Payroll Update").Cells(j, 2) Then
                            Sum = 0
                            For e = 2 To Total_rows_DTR
                                If Worksheets("DTR").Cells(e, 3) >= Worksheets("Payroll - Extra").Cells(q, 1) And Worksheets("DTR").Cells(e, 3) < Worksheets("Payroll Update").Cells(j, 2) And Worksheets("DTR").Cells(e, 2) = Worksheets("Payroll Update").Cells(j, 1) Then
                                    Sum = Sum + Worksheets("DTR").Cells(e, 28)
                                End If
                            Next e
                            If Worksheets("Payroll - Extra").Cells(q, h) <> Sum And IsFormula(Worksheets("Payroll - Extra").Cells(q, h)) = True Then
                                Worksheets("Payroll - Extra").Cells(q, h) = Worksheets("Payroll - Extra").Cells(q, h) + Worksheets("DTR").Cells(e, 28)
                            End If
                        End If
                    Next q
                End If
            Next h
        End If
        For w = 2 To Total_rows_PTS
            If Worksheets("Payroll Tables and Settings").Cells(w, 1) = Worksheets("Payroll Update").Cells(j, 1) Then
                Worksheets("Payroll Tables and Settings").Cells(w, 4) = Worksheets("Payroll Update").Cells(j, 5)
            End If
        Next w   
    End If
Next j

但是,我的问题是,当我单击另一个工作表时,deactivate事件会运行,并不断切换我单击的工作表以及代码中编写的工作表Worksheets("Payroll - Extra")。这是一个问题,因为每次工作表切换我点击的工作表的事件也可能触发。这样的示例是当我单击带有activate或deactivate事件的工作表时。这导致我的代码在某些时候出错。我怎么得到这个?

以下是尝试使问题更清晰的流程图: Flowchart

0 个答案:

没有答案