Code breaks despite using "On error resume next"

abdulbasitb

New Member
Joined
Apr 21, 2015
Messages
17
I don't understand why the code below breaks at line 14. (xComp = xCell.Value)
It passes that line once but breaks the second time in the loop.

I'm using it as UDF like this: =GetFirstFactor(A1,B2:B10)
Note: There are some blank cells in the range B2:B10 with the value = ""

VBA Code:
Function GetFirstFactor(RefValue As Range, xArr As Range) As Variant
'The purpose of this function is to find the first value in range xArr which is a factor of RefValue.
'eg. If values in xArr = 4,6,8,3,12 and RefValue = 18, then output will be 6.

    Dim xVal As Double, xComp As Double, xCell As Variant
   
    On Error Resume Next
        xVal = RefValue.Cells(1).Value2
    On Error GoTo 0
   
    For Each xCell In xArr.Cells
   
        On Error Resume Next
            xComp = xCell.Value
            Err.Clear
        On Error GoTo 0
       
        On Error GoTo NextRow
        If xVal Mod xComp < 0.001 Then
            GetFirstFactor = xVal
            Exit Function
        End If
NextRow:
    Err.Clear
    On Error GoTo 0
    Next
End Function
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Is using xCell as Variant makes a difference?
 
Upvote 0
Is using xCell as Variant makes a difference?
Thanks Zot for prompt response.

In the original code above, I'm using xCell as a Variant.
I've also tried xCell as Range and also tried not declaring it.

None of these work.
 
Upvote 0
Thanks Zot for prompt response.

In the original code above, I'm using xCell as a Variant.
I've also tried xCell as Range and also tried not declaring it.

None of these work.
On final On Error Goto NextRow, you have Err.Clear. What happened when no error? Code goes through On Error Goto NextRow and then Exit Function. The error is still flagged.

I think that is the problem. Before Exit Function, try add On Error GoTo 0.
 
Upvote 0
Hi,
untested but see if these changes to your function resolve your issue

VBA Code:
Function GetFirstFactor(ByVal RefValue As Range, ByVal xArr As Range) As Variant
    'The purpose of this function is to find the first value in range xArr which is a factor of RefValue.
    'eg. If values in xArr = 4,6,8,3,12 and RefValue = 18, then output will be 6.
    
    Dim xVal        As Double, xComp As Double
    Dim xCell       As Range
    
    With RefValue.Cells(1)
        If Not IsNumeric(.Value2) Then Exit Function
        xVal = .Value2
    End With
    
    For Each xCell In xArr.Cells
        On Error GoTo NextRow
        If IsNumeric(xCell.Value) Then
            xComp = xCell.Value
            If xVal Mod xComp < 0.001 Then
                GetFirstFactor = xVal
                Exit Function
            End If
        End If
NextRow:
        Err.Clear
    Next
        
End Function

Dave
 
Upvote 0
Solution
Thanks dmt32, that worked! (y)
FYI Zot, tried the solution in second message but didn't work.
Thanks for suggestions.
Forget my suggestion. There is no logic to it. The blood flow in my brain was reversed for no apparent reason ?
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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