readfile 同步:EXCEL工作表中自动求和并换行

来源:百度文库 编辑:中财网 时间:2024/05/01 12:48:44
工作表中自动求和并换行

为了录入方便,可以在每次输入时按键盘自动换行并求和,不用手工插入行

step1:

'在sheet1代码窗口中输入以下代码

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rowx As Integer
rowx = Sheet1.Range("a65536").End(xlUp).Row
Sheet1.Range("d" & rowx).Value = Application.WorksheetFunction.Sum(Sheet1.Range("d2:d" & rowx - 1))
End Sub

step2:

'添加模块,输入代码

Option Explicit
Public Sub addrow()
Dim sel As Integer
sel = Application.ActiveCell.Row
Sheet1.Rows(sel + 1).Insert
End Sub

step3:

在thisworkbook中输入代码,使之能达到打开工作薄自动运行

Option Explicit

Private Sub Workbook_Open()
Application.OnKey "{ENTER}", "addrow"
End Sub

OK,在操作时,按小键盘的enter回车键就能自动换行并求和了