数组差异 IF/OR VBA 脚本

Array Difference IF/OR VBA Script

有存储数组的脚本,如果它发生变化,它会发现差异。现在我需要它来获取差异并检查相邻单元格是否为“John 或 Mary”并将数组值差异乘以:(如果“John”= Array Value Diff。* PO#1)或(如果“Mary” = 数组值差异 * PO#2)。这是我需要写入 Sheet 1 的最终值,现在只是数组值差异。价值。

模块 1 中的代码

Public myArr()
Public Sub PopulateMyArr()
    myArr = Sheet4.Range("I6:I500").Value
End Sub

本工作簿中的代码

Private Sub Workbook_Open()
    PopulateMyArr
End Sub

Sheet4

中的代码
Private Sub ToggleButton1_Click()

End Sub

Private Sub Worksheet_Calculate()

Dim keyCells As Range
On Error GoTo safeexit
Application.EnableEvents = False

Set keyCells = Me.Range("I6:I500")

If Worksheets("BA_Size").ToggleButton1.Value = True Then
Dim i As Integer
For i = 1 To UBound(myArr)
If keyCells(i, 1).Value <> myArr(i, 1) Then
   
nextrow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row + 1
Sheet1.Cells(nextrow, "A").Value = (keyCells(i, 1).Value - myArr(i, 1))
End If
Next i
End If
safeexit:
PopulateMyArr
Application.EnableEvents = True
End Sub

未测试:

Private Sub Worksheet_Calculate()

    Dim keyCells As Range, i As Long, diff, cKey As Range
    
    'exit if togglebutton not on
    If Not Worksheets("BA_Size").ToggleButton1.Value Then Exit Sub
    
    On Error GoTo safeexit
    Application.EnableEvents = False
    
    Set keyCells = Me.Range("I6:I500")
    nextrow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row + 1
    
    For i = 1 To UBound(myArr)
        Set cKey = keyCells(i, 1)
        If cKey.Value <> myArr(i, 1) Then
            diff = (cKey.Value - myArr(i, 1))
            'check value in Col K
            Select Case cKey.EntireRow.Columns("K").Value
                Case "John": diff = diff * cKey.EntireRow.Columns("N").Value
                Case "Mary": diff = diff * cKey.EntireRow.Columns("O").Value
                Case Else: diff = 0
            End Select
            Sheet1.Cells(nextrow, "A").Value = diff
            nextrow = nextrow + 1
        End If
    Next i
      
safeexit:
    PopulateMyArr
    Application.EnableEvents = True
End Sub