VB请修改以下代码

我要的就是象EXCEL 的内部函数 一样来使用。它的功能就是对含有文字和字母的多个不连续的单元格区域求和。操作方法:AA(各个求和的区域,并用逗号分开),恳请大侠多多帮忙,我会多多加分的!请求大侠再优化以下代码(以下代码只能对一个区域求和)。Public Function 求和(区域 As Range) As SingleOn Error Resume NextDim Str1 As String, Str2 As String, Str3 As String, Str4 As StringDim i As IntegerDim sum As Integersum = 0For Each cell In 区域 Str1 = cell.Text If Str1 <> "" Then Str2 = "1234567890.+-*⼀ ()×" Str4 = "" For i = 1 To Len(Str1) Str3 = Mid(Str1, i, 1) If InStr(1, Str2, Str3) > 0 Then Str4 = Str4 & Str3 End If Next Str4 = Replace(Str4, "×", "*") Str4 = Evaluate(Str4) If IsNumeric(Str4) = True Then sum = sum + Str4 End If End IfNext cell 求和 = sumEnd Function
2026年09月25日 12:27
有1个网友回答
网友(1):

调用示例
=求和(B3:C4,D3:D7,B6:C7,C8:D8)

Public Function 求和(ParamArray 区域()) As Single
On Error Resume Next

Dim Str1 As String, Str2 As String, Str3 As String, Str4 As String
Dim i As Integer
Dim sum As Integer
sum = 0
For Each 区域1 In 区域
For Each cell In 区域1
Str1 = cell.Text

If Str1 <> "" Then
Str2 = "1234567890.+-*/ ()×"
Str4 = ""
For i = 1 To Len(Str1)
Str3 = Mid(Str1, i, 1)
If InStr(1, Str2, Str3) > 0 Then
Str4 = Str4 & Str3
End If
Next

Str4 = Replace(Str4, "×", "*")
Str4 = Evaluate(Str4)
If IsNumeric(Str4) = True Then
sum = sum + Str4
End If
End If
Next cell
Next 区域1
求和 = sum
End Function

记得加分啊 (先提高悬赏,再加分呵呵)