Home » excel » excel – Find sign change in array

excel – Find sign change in array

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have data in excel which starts with negative values, passes zero and reaches a certain positive value. I put this particular set of data in an array dataset1 = {-9.5,-7,-5.25,-3,-1.1,0,1,5,20,50} (just as an example, the values aren’t linearly spaced) to do some calculations. But what I need to know is the index of the last number which has a sign (is negative).
How can I do that? Sadly there is no function like IsSigned(..)=true.

How to&Answers:

If you want to do it without a loop

Sub test()

    Dim dataset1 As Variant

    dataset1 = Array(-9.5, -7, -5.25, -3, -1.1, 0, 1, 5, 20, 50)

    Debug.Print UBound(Filter(dataset1, "-")) + 1

End Sub

Answer:

My solution looks like this:

'Find last signed value
 For i = LBound(concarraycleared, 2) To UBound(concarraycleared, 2)
     If concarraycleared(2, i) < 0 Then
         LastSignedIndex = i
     Else
         Exit For
     End If
 Next i

Couple of limitations: Array needs to be vertical, can only find the last signed value one time, values need to be sorted in ascending order. You can not reverse search order. This is pretty specific solution tailored to my problem, so the above solution should be regarded fist.