将单元格的前一个值与当前值进行比较。

问题描述 投票:0回答:1

O2.O20 "范围内的每个单元格都有数值。O20 "范围内的每个单元格都有数值。每一个单元格旁边都有一个单元格,根据 "O2.020 "中存在的数值,也被填充了数值。020". 例如:"O2:020 "中的数值。如果 "O2"=10. 2 那么它旁边的单元格 "P2"=1000 但是 "P2"=500 然后 "P2"=600 然后 "P2"=50 简而言之,"P2 "可以取任何正的自然值。我想计算 "O2 "只要 "O2 "的值不变,"P2 "以前取的值和现在可以取的值之间的差值。如果 "O2 "的值发生了变化,那么这个差值对我来说就不重要了。例如: 如果 "O2"=10.2,"P2"=50 然后 "O2"=10,"P2"=3000,在这种情况下,我不想知道区别, 因为 "O2 "对两个单元格来说是不一样的。

excel vba record
1个回答
0
投票

我希望我能理解你的问题。请看这个解决方案.它使用的是 Option Base 1.更新的程序,用于将差值写入 Q 列.如果不需要信息,请删除或 Rem 最后的 MsgBox 行。

Option Base 1
Private Sub Worksheet_Change(ByVal Target As Range)
    'Static declaration to safe the previous values and change status
    Static vO As Variant
    Static vP As Variant
    Static bolOChanged() As Boolean
    
    'Set up the ranges
    Dim rngO As Range, rngP As Range, rngQ As Range
    Set rngO = ThisWorkbook.ActiveSheet.Range("O2:O20")
    Set rngP = ThisWorkbook.ActiveSheet.Range("P2:P20")
    Set rngQ = ThisWorkbook.ActiveSheet.Range("Q2:Q20") 'Range for results
    
    'If the change is not in the range then ignore
    If Intersect(Union(rngO, rngP), Target) Is Nothing Then Exit Sub
    
    'Prevent unhandelt multiply changes. If multiply changes required than the
    'Target range shall be loop through
    If Target.Cells.Count > 1 Then
        Application.EnableEvents = False
        rngO.Value = vO
        rngP.Value = vP
        Application.EnableEvents = True
        MsgBox "You cannot change more the one cell in the range of: " & Union(rngO, rngP).Address
        Exit Sub
    End If
    
    'At the firs occasion the current status has to be saved
    If VarType(vO) < vbArray Then
        vO = rngO.Value
        vP = rngP.Value
        ReDim bolOChanged(1 To UBound(vO))
    End If
    
    Dim iIndex As Long 'Adjust the index of the array to the Row of Target cell
    iIndex = Target.Row - rngO(1).Row + 1
    
    If Not Intersect(rngO, Target) Is Nothing Then
        'Change was in O range, so next P change shall be ignored
        bolOChanged(iIndex) = True
    Else
        'rngP changed
        If bolOChanged(iIndex) Then
            'There was a previous O range change, ignore
            bolOChanged(iIndex) = False 'Delete the recent change flag
            vP(iIndex, 1) = Target.Value 'Store the value
        Else
            rngQ(iIndex).Value = Target.Value - vP(iIndex, 1)
            MsgBox "Value change from: " & vP(iIndex, 1) & ", to: " & Target.Value & ". Difference is: " & Target.Value - vP(iIndex, 1)
            vP(iIndex, 1) = Target.Value 'Store the value
        End If
    End If
End Sub

更新:这个版本可以使用乘法条目。

Option Base 1
Private Sub Worksheet_Change(ByVal Target As Range)
    'Static declaration to safe the previous values and change status
    Static vO As Variant
    Static vP As Variant
    Static bolOChanged() As Boolean
    
    'Set up the ranges
    Dim rngO As Range, rngP As Range, rngQ As Range
    Set rngO = ThisWorkbook.ActiveSheet.Range("O2:O20")
    Set rngP = ThisWorkbook.ActiveSheet.Range("P2:P20")
    Set rngQ = ThisWorkbook.ActiveSheet.Range("Q2:Q20") 'Range for results
    
    'If the change is not in the range then ignore
    If Intersect(Union(rngO, rngP), Target) Is Nothing Then Exit Sub
        
    'At the firs occasion the current status has to be saved
    If VarType(vO) < vbArray Then
        vO = rngO.Value
        vP = rngP.Value
        ReDim bolOChanged(1 To UBound(vO))
    End If
    
    Dim iIndex As Long 'Adjust the index of the array to the Row of Target cell
    Dim item As Variant
    For Each item In Target
        iIndex = item.Row - rngO(1).Row + 1
        If Not Intersect(rngO, item) Is Nothing Then
            'Change was in O range, so next P change shall be ignored
            bolOChanged(iIndex) = True
        Else
            'rngP changed
            If bolOChanged(iIndex) Then
                'There was a previous O range change, ignore
                bolOChanged(iIndex) = False 'Delete the recent change flag
                vP(iIndex, 1) = item.Value 'Store the value
            Else
                rngQ(iIndex).Value = item.Value - vP(iIndex, 1)
                MsgBox "Value changed in cell " & item.Address & " from: " & vP(iIndex, 1) & ", to: " & item.Value & ". Difference is: " & item.Value - vP(iIndex, 1)
                vP(iIndex, 1) = item.Value 'Store the value
            End If
        End If
    Next item
End Sub

0
投票

这个解决方案使用更多的工作表列来存储以前的值,以便与实际值进行比较。在我的例子中,O2和O3单元格中的值将始终是相同的。Columns O and P are the source of values. Previous values are stored in columns R and S. The difference is stored in column T

Sub Populate_OandP()

'Store previous values
Call PreviousValues

'This code just simulates the data population in columns O and P
                Dim intRndNumber As Integer

                Range("O2").Value = 10.2
                Range("O3").Value = 10

                intRndNumber = Int((10 - 1 + 1) * Rnd + 1)

                For i = 4 To 20
                    intRndNumber = Int((10 - 1 + 1) * Rnd + 1)
                    Cells(i, 15).Value = intRndNumber * 10
                Next i

                For i = 2 To 20
                    intRndNumber = Int((10 - 1 + 1) * Rnd + 1)
                    Cells(i, 16).Value = intRndNumber * 10
                Next i

'Check differences
Call CheckDifferenceIfOChanges    

End Sub

Sub PreviousValues()

For i = 2 To 20
    Cells(i, 18).Value = Cells(i, 15).Value
    Cells(i, 19).Value = Cells(i, 16).Value
Next i

End Sub

Sub CheckDifferenceIfOChanges()

For i = 2 To 20
    If Cells(i, 18).Value = Cells(i, 15).Value Then
        Cells(i, 20).Value = Cells(i, 19).Value - Cells(i, 16).Value
    Else: Cells(i, 20).Value = "O columns value changed"
    End If
Next i

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.