Private Sub CommandButton1_Click()
Dim i As Integer
'获取非空格的行数
endrow = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row
'创建数组
arr = Sheets("sheet1").Range("a2:a" & endrow, "b2:b" & endrow)
Dim sum1 As Variant
Dim sum2 As Variant
sum1 = 0
sum2 = 0
'数组中取值叠加
For i = 1 To UBound(arr)
sum1 = sum1 + arr(i, 1)
Next i
Range("a1").Value = sum1
For i = 1 To UBound(arr)
sum2 = sum2 + arr(i, 2)
Next i
Range("b1").Value = sum2
End Sub
Private Sub CommandButton2_Click()
Dim i As Integer
endrow = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row
arr = Sheets("sheet1").Range("a2:a" & endrow)
Dim sum As String
sum = ""
For i = 1 To UBound(arr)
sum = sum & arr(i, 1)
Next i
Range("d1").Value = sum
MsgBox (sum)
End Sub
网友评论