关于Excel求差的问题,求大神帮忙用VBA代码,解决一下,

2025-05-20 02:08:47
推荐回答(1个)
回答1:

在 Worksheet.Change 中加入以下代码便可实现你要的效果:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngRow As Long
    
    lngRow = Target.Row
    
    If lngRow = 5 Then
        Dim lngCol As Long
        
        lngCol = Target.Column
        
        If (lngCol = 3) Or (lngCol = 4) Then
            Application.EnableEvents = False
            
            If lngCol = 3 Then
                With Target.Offset(, 1)
                    .FormulaR1C1 = "=R[-1]C[-1]-RC[-1]"
                    .Value2 = .Value2
                End With
            Else
                With Target.Offset(, -1)
                    .FormulaR1C1 = "=R[-1]C-RC[1]"
                    .Value2 = .Value2
                End With
            End If
            
            Application.EnableEvents = True
        End If
    End If
End Sub


动画效果: