excle自编公式方法

操作很简单,工具-->巨集--》VB编辑器

选中 Microsoft 物件-->插入-->模组

下面的为一段代码,差不多明白怎么操作rang和返回。

不太清楚VBA是不是有点问题,我用checkCount="#??" return 这样的方式返回,有点问题,只能使用我强烈鄙视的goto才正常了 

Private Function checkCount(target As Range, countCell As Range, lookupRange As Range)

Dim cell  As Range
Dim xsum  As Double
Dim xval As Double
Dim result As String


If (VarType(countCell(1))) <> vbDouble Then
   result = "#??"
   GoTo 888
Else
   xval = countCell(1).Value
End If

Dim i As Integer
xsum = 0
i = 1
While (i < target.Count)
    If (VarType(target(i)) = vbDouble) Then
        xsum = xsum + target(i).Value
        If (xval <= xsum) Then
           If (lookupRange.Count < i) Then
              result = "#???"
              GoTo 888
           Else
              result = lookupRange(i).Value
              GoTo 888
           End If
        End If
    End If
    i = i + 1
Wend

888:
  checkCount = result

End Function
原文地址:https://www.cnblogs.com/wonder223/p/3620473.html