|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
下面是自动求和的代码,如果选择两个不连续区域,只能对最后一次选区单元格数据进行求和,求各位前辈看看怎么能实现对所有选区的单元格进行计算,谢谢!
Sub 表格选区单元格求和()
Dim oRng As Range, C As Cell, oTable As Table, dSum As Double
Dim sCellValue, arr(), i, j
On Error Resume Next
With Selection
For Each C In .Cells
sCellValue = C.Range.Text '把单元格结尾字符去掉
sCellValue = VBA.Replace(sCellValue, Chr(13) & Chr(7), "") '判断单元格内容是否是数值
If VBA.IsNumeric(sCellValue) Then
ReDim Preserve arr(i)
arr(i) = sCellValue
dSum = dSum + arr(i)
i = i + 1
End If
Next
End With
MsgBox "选择的数据是:" & Join(arr, " ") & vbCrLf & "共" & i & "个数据" & vbCrLf & "求和结果为:" & dSum
End Sub
|
|