質問

It might be a simple question but I couldn't find an answer yet I have a cell which contains cells range as a text at the format of A1:A15. I am trying to write a function to receive the actual range of the cells (A1:A15) but it only gives me the address at which the actual cell is (B4).

how can I get the range itself and not the address of the cell?

the function is:

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

thank you in advance

役に立ちましたか?

解決

Try this one:

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

or better:

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

Also note that when you're using Dim i, k As Integer, only k has type Integer, while i is Variant. So, you should use Dim i As Integer, k As Integer instead.

And read: How to avoid using Select/Active statements

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top