how to test is dynamic array contains a zero

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
i am creating Dynamic arrays from ranges and need to check if it contains a zero, if it does do this, else do that..

whats the best way to test if a returned dynamic array contains a zero? thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I believe COUNTIF does not work with arrray...
Try this:
=IF(OR(array=0), do this, do that)
 
Upvote 0
I believe COUNTIF does not work with arrray...
Try this:
=IF(OR(array=0), do this, do that)
Curious, why it works in mine?
1666323111939.png
 
Upvote 0
Curious, why it works in mine?
View attachment 76713
Because this
i am creating Dynamic arrays from ranges and need to check if it contains a zero, if it does do this, else do that..

whats the best way to test if a returned dynamic array contains a zero? thanks
I though OP want "Dynamic arrays from range" not "Dynamic range".
While your formula works for "Dynamic range": A1:B5
 
Upvote 0
One possibility
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    Arr = Range("A1", Cells(Rows.Count, "B").End(xlUp)) '<< change range to suit
    
    For i = 1 To UBound(Arr, 1)
        For j = 1 To UBound(Arr, 2)
            If Arr(i, j) = 0 Then
                k = k + 1
            End If
        Next j
    Next i
    
    If k > 0 Then
        'Do something
    Else
        'Do something else
    End If
End Sub
 
Upvote 0
One possibility
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    
End Sub
Why do not stop right after zero found?
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    Arr = Range("A1", Cells(Rows.Count, "B").End(xlUp)) '<< change range to suit
     For i = 1 To UBound(Arr, 1)
        For j = 1 To UBound(Arr, 2)
            If Arr(i, j) = 0 Then
                 Do something
                  Exit sub 
            End If
        Next j
    Next i
   Do something else
  End Sub
Or using FIND should be simpler
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    Arr = Range("A1", Cells(Rows.Count, "B").End(xlUp)) '<< change range to suit
    If not Arr.Find 0 is nothing then 
           Do something
     else
         Do Something else
    end if
end sub
 
Upvote 0
Why do not stop right after zero found?
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    Arr = Range("A1", Cells(Rows.Count, "B").End(xlUp)) '<< change range to suit
     For i = 1 To UBound(Arr, 1)
        For j = 1 To UBound(Arr, 2)
            If Arr(i, j) = 0 Then
                 Do something
                  Exit sub 
            End If
        Next j
    Next i
   Do something else
  End Sub
Or using FIND should be simpler
VBA Code:
Sub Test_For_Zero()
    Dim Arr, i As Long, j As Long, k As Long
    Arr = Range("A1", Cells(Rows.Count, "B").End(xlUp)) '<< change range to suit
    If not Arr.Find 0 is nothing then 
           Do something
     else
         Do Something else
    end if
end sub
@bebo021999 yes, good point! 🙂
 
Upvote 0
Excel Formula:
=IF(COUNTIF(A1#,0),dothis,dothat)
just using the COUNTIF(A1#,0)... this works in a direct formula against a dynamic array like you show in your example and i tested with Sequence()

but fails to return a scalar Count result within a LET (which is where I will use it on), I am expecting a single scalar value, >0 or 0,

within a LET it returns an array of #VALUE! errors... why? how can i return a scalar within a LET

Rich (BB code):
=LET(x, SEQUENCE(4,,1,1),
COUNTIF(x,0)
)
 
Last edited:
Upvote 0
How about
Excel Formula:
=LET(x, SEQUENCE(4,,1,1),COUNT(FILTER(x,x=0)))
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top