I'm new to Excel VBA, but I can't find a solution for this issue.
I'm trying to create a FOR EACH loop which reads floats inside a range. Inside this loop i should understand if the floats in the cell are 0s or floats greater than 0 and, in the second scenario, put this float in a list/array.
the function should return the first number of the list or 0, if in the range there aren't any floats greater than 0.
in a python env, the code would look like this:
rng = list of floats in the range
empty_list = []
for element in rng:
if element>0:
empty_list.append(element)
if len(empty_list)>0:
return empty_list[0]
if len(empty_list)==0:
return 0
what I wrote so far in VBA:
Function getNumber()
Dim rng As Range
Dim cell As Range
Dim ArrayLen As Integer
Dim arr() As Integer
Dim counter As Integer
ReDim arr(1)
counter = 0
For Each cell In rng
If cell > 0 Then
arr(counter) = cell.Value
counter = counter + 1
End If
Next
ArrayLen = UBound(arr) - LBound(arr) + 1
If ArrayLen = 0 Then
getNumber = 0
End If
If ArrayLen > 0 Then
getNumber = 10
End If
End Function
Thanks in advance to anybody!