Vba单元格输入数字自动累计求和
在某个单元格连续输入不同的数字,则该单元格或另一个单元格连续显示累计和.
附图:
代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
On Error Resume Next
'判断回车
Dim x As Long
x = GetKeyState(&HD) ' Enter's virtual key code is &H0D
If (x And &H80) = &H80 Then ' 按下了回车
'限定列范围
If (Target.Column = 2 Or Target.Column = 4 Or Target.Column = 6 Or Target.Column = 8 Or Target.Column = 10) And Target.Row < 13 Then
Static TempValue
If TempValue = "" Then
TempValue = Selection.Text
End If
If TempValue = Selection.Text Then
'要进行的操作
Cells(ActiveCell.Row + 14, ActiveCell.Column).Formula = "=" & Replace(Cells(ActiveCell.Row + 14, ActiveCell.Column).Formula, "=", "") & "+" & Cells(ActiveCell.Row - 1, ActiveCell.Column)
Else
'改变时要进行的操作
Cells(ActiveCell.Row + 14, ActiveCell.Column).Formula = "=" & Replace(Cells(ActiveCell.Row + 14, ActiveCell.Column).Formula, "=", "") & "+" & Cells(ActiveCell.Row - 1, ActiveCell.Column)
TempValue = Cells(ActiveCell.Row - 1, ActiveCell.Column) + Selection.Text
End If
End If
End If '判断回车
End Sub
以上由vba助手原创, http://www.excelword.net
PAGE
- 1 -