从格式为(A1:A15)的单元格中获取范围

时间:2014-05-14 16:16:58

标签: excel vba excel-vba range cells

这可能是一个简单的问题,但我还没有找到答案 我有一个包含单元格范围的单元格,格式为A1:A15。 我正在尝试编写一个函数来接收单元格的实际范围(A1:A15),但它只给出了实际单元格所在的地址(B4)。

如何获取范围本身而不是单元格的地址?

功能是:

Function IsSick(ByVal Arr As Range) As Integer
   Dim i, k As Integer
   k = 0
   Arr.Select
   For i = 2 To Arr.Count
       If Arr(i) >= 37 Then k = k + 1
   Next i
   IsSick = k
End Function

提前谢谢

1 个答案:

答案 0 :(得分:3)

试试这个:

Function IsSick(ArrAdr As Range) As Integer
    Dim i As Integer, k As Integer
    Dim arr As Range
    Set arr = Range(ArrAdr.Value)
    k = 0
    For i = 2 To arr.Count
        If arr(i) >= 37 Then k = k + 1
    Next i
    IsSick = k
End Function

或更好:

Function IsSick(ArrAdr As Range) As Integer
    IsSick = Application.CountIf(Range(ArrAdr.Value), ">=37")
End Function

另请注意,当您使用Dim i, k As Integer时,只有k的类型为Integer,而iVariant。因此,您应该使用Dim i As Integer, k As Integer代替。

并阅读:How to avoid using Select/Active statements