关于一段统计单元格公式的excel VBA代码
如果在excel表格中输入=0.15*0.23+0.12*0.23那么统计的计算式的数量就是2,如果输入=0.15*0.23*2+0.12*0.23那么统计的计算式的数量...
如果在excel表格中输入=0.15*0.23+0.12*0.23那么统计的计算式的数量就是2,如果输入=0.15*0.23*2+0.12*0.23那么统计的计算式的数量就是2+1=3
就是统计一下有几个两位数的乘法公式,
=0.15*0.15*2+0.15*0.15,我的意思是如果有第三个数的把第三个数加上,如果没有第三个数的就加1
实际的应用是这样的,我是要统计钢板的数量,0.15*0.15是钢板的面积,*2是块数
下面的代码,怎么用不了呢,请指导
Function Cnt2(r As Range)
Dim n%, l%
Dim arr(0 To 2)
cFor = r.Formula
arr = Split(Replace(cFor, "=", ""), "+")
For i = 0 To UBound(arr)
brr = Split(arr(i), "*")
Select Case UBound(brr)
Case Is = 0
n = n + 1
Case Is = 1
n = n + 1
Case Is = 2
n = n + brr(2)
End Select
Next
Cnt = n
End Function 展开
就是统计一下有几个两位数的乘法公式,
=0.15*0.15*2+0.15*0.15,我的意思是如果有第三个数的把第三个数加上,如果没有第三个数的就加1
实际的应用是这样的,我是要统计钢板的数量,0.15*0.15是钢板的面积,*2是块数
下面的代码,怎么用不了呢,请指导
Function Cnt2(r As Range)
Dim n%, l%
Dim arr(0 To 2)
cFor = r.Formula
arr = Split(Replace(cFor, "=", ""), "+")
For i = 0 To UBound(arr)
brr = Split(arr(i), "*")
Select Case UBound(brr)
Case Is = 0
n = n + 1
Case Is = 1
n = n + 1
Case Is = 2
n = n + brr(2)
End Select
Next
Cnt = n
End Function 展开
2个回答
展开全部
主要问题有两点:一是Split函数不能赋值给定下标的数组,所以定义arr时不能定下标范围;二是返回值Cnt = n语句中少了一个2,应为Cnt2 = n。我把代码给你简化了一下:
Function Cnt2(r As Range) As Integer
Dim i%, n%, arr() As String, brr() As String
arr = Split(Replace(r.Formula, "=", ""), "+")
For i = 0 To UBound(arr)
brr = Split(arr(i), "*")
If UBound(brr) = 2 Then
Cnt2 = Cnt2 + brr(2)
Else
Cnt2 = Cnt2 + 1
End If
Next
End Function
Function Cnt2(r As Range) As Integer
Dim i%, n%, arr() As String, brr() As String
arr = Split(Replace(r.Formula, "=", ""), "+")
For i = 0 To UBound(arr)
brr = Split(arr(i), "*")
If UBound(brr) = 2 Then
Cnt2 = Cnt2 + brr(2)
Else
Cnt2 = Cnt2 + 1
End If
Next
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询