帮忙用VBA写一个Excel求和程序。

2025-05-10 03:57:13
推荐回答(3个)
回答1:

但是有个问题,行数和列数不一定。
不用要用户去数据行列,可以用以下方式得到:
nLR = ActiveSheet.Cells.SpecialCells(xlLastCell).Row '最后一行(与连续与否无关)
nLC = ActiveSheet.Cells.SpecialCells(xlLastCell).Column '最后一列(与连续与否无关)

回答2:

Sub mesum()
Dim a As Range
Dim b As Byte
Dim c As Byte
Dim d As Byte
Dim e As Byte
On Error Resume Next
Set a = Application.InputBox("请用鼠标选择要求和的区域!", Type:=8)
b = a.End(xlDown).Row - a.Row + 1
c = a.End(xlToRight).Column - a.Column + 1
For e = 1 To c
For d = 1 To b
a.Cells(b + 1, e) = a.Cells(d, e) + a.Cells(b + 1, e)
Next
Next
For e = 1 To b
For d = 1 To c
a.Cells(e, c + 1) = a.Cells(e, d) + a.Cells(e, c + 1)
Next
Next
End Sub

回答3:

game57的方法应该会更好些,用inputbox,直接选择要处理的区域。