IF(ISERROR) in an array

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have these formulae in cells:

Code:
=IF(ISERROR(A1/B1),0,A1/B1)

=IF(ISERROR(C1/D1),0,C1/D1)


How can I create this in a VBA array?

For Example:

Code:
Dim DataArray() As Variant

DataArray = Cells(1,1).CurrentRegion.Value

Dim MyArray(1 To 10, 1 To 2) As Variant

Dim Counter As Integer

On Error GoTo ErrHandler

For Counter = 1 To 10

    MyArray(Counter, 1) = DataArray(Counter, 1) / DataArray(Counter, 2)
    MyArray(Counter, 2) = DataArray(Counter, 3) / DataArray(Counter, 4)

Next Counter

Exit Sub

ErrHandler:

MyArray(Counter, 1) = 0
MyArray(Counter, 2) = 0

Resume Next

If only B1 OR D1 is zero, it will error out but how can my array know which one errors?

Thanks
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
a division will error if the denominator is 0 or if a value is not numeric... i would think those are the only situations. So you can check the values before the division and if they are not numbers or denominator > 0 then do something else. I kinda don't follow what you mean by "how will my array know" put a value of -1 for errors or something, it is a variant array so put a string value that says "error" The logic is up to you.
 
Last edited:
Upvote 0
One way

Code:
Dim DataArray() As Variant

DataArray = Cells(1, 1).CurrentRegion.Value
Dim MyArray(1 To 10, 1 To 1) As Variant
Dim Counter As Integer
For Counter = 1 To 10
    On Error Resume Next
    MyArray(Counter, 1) = DataArray(Counter, 4) / DataArray(Counter, 5)
    If Err.Number <> 0 Then
        MyArray(Counter, 1) = 0
        On Error GoTo 0
    End If
Next Counter
'Just to check - optional
Range("F1:F10") = MyArray

M.
 
Upvote 0
One way

Code:
Dim DataArray() As Variant

DataArray = Cells(1, 1).CurrentRegion.Value
Dim MyArray(1 To 10, 1 To 1) As Variant
Dim Counter As Integer
For Counter = 1 To 10
    On Error Resume Next
    MyArray(Counter, 1) = DataArray(Counter, 4) / DataArray(Counter, 5)
    If Err.Number <> 0 Then
        MyArray(Counter, 1) = 0
        On Error GoTo 0
    End If
Next Counter
'Just to check - optional
Range("F1:F10") = MyArray

M.

Thanks but would I have to do TWO loops, one to check B1 <> 0 and another for D1 <> 0?
 
Upvote 0
Try

Code:
Sub aTest()
    Dim DataArray() As Variant, Counter As Integer, MyArray(1 To 10, 1 To 2) As Variant

    DataArray = Cells(1, 1).CurrentRegion.Value

    For Counter = 1 To 10
        On Error Resume Next
        MyArray(Counter, 1) = DataArray(Counter, 1) / DataArray(Counter, 2)
        If Err.Number <> 0 Then MyArray(Counter, 1) = 0
        On Error GoTo 0
        
        On Error Resume Next
        MyArray(Counter, 2) = DataArray(Counter, 3) / DataArray(Counter, 4)
        If Err.Number <> 0 Then MyArray(Counter, 2) = 0
        On Error GoTo 0
    Next Counter
    
    'Just to check
    Range("G1:H10") = MyArray
End Sub

M.
 
Upvote 0
Try

Code:
Sub aTest()
    Dim DataArray() As Variant, Counter As Integer, MyArray(1 To 10, 1 To 2) As Variant

    DataArray = Cells(1, 1).CurrentRegion.Value

    For Counter = 1 To 10
        On Error Resume Next
        MyArray(Counter, 1) = DataArray(Counter, 1) / DataArray(Counter, 2)
        If Err.Number <> 0 Then MyArray(Counter, 1) = 0
        On Error GoTo 0
        
        On Error Resume Next
        MyArray(Counter, 2) = DataArray(Counter, 3) / DataArray(Counter, 4)
        If Err.Number <> 0 Then MyArray(Counter, 2) = 0
        On Error GoTo 0
    Next Counter
    
    'Just to check
    Range("G1:H10") = MyArray
End Sub

M.

Thanks, your suggestion works well for this example.

However, if I had a different condition, like:

Code:
If MyArray(Counter, 1) > 10 Then


and MyArray(Counter, 1) was NOT numeric but a letter, for some reason it does NOT error out!

a division will error if the denominator is 0 or if a value is not numeric... i would think those are the only situations. So you can check the values before the division and if they are not numbers or denominator > 0 then do something else. I kinda don't follow what you mean by "how will my array know" put a value of -1 for errors or something, it is a variant array so put a string value that says "error" The logic is up to you.

Therefore I used Cerfani's suggestion to check for numeric before proceeding.
 
Last edited:
Upvote 0
Don't understand what you are saying.

All the values in MyArray are the results of a division
MyArray(Counter, 1) = DataArray(Counter, 1) / DataArray(Counter, 2)
MyArray(Counter, 2) = DataArray(Counter, 3) / DataArray(Counter,4)

They cannot ever be a letter.

M.
 
Upvote 0
Don't understand what you are saying.

All the values in MyArray are the results of a division
MyArray(Counter, 1) = DataArray(Counter, 1) / DataArray(Counter, 2)
MyArray(Counter, 2) = DataArray(Counter, 3) / DataArray(Counter,4)

They cannot ever be a letter.

M.


Sorry but if my original formula was different, say:

Code:
=IF(ISERROR(IF(A2>Z100,E2,E2/F2),0,IF(A2>Z100,E2,E2/F2))

then if A2 was a letter, say p but Z100 is the value 1000, it does NOT error.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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