VBA Function Not Working

animas

Active Member
Joined
Sep 28, 2009
Messages
396
Code:
Public Function Test(iRange As Range)

Dim tmp As Integer

If Application.IsNumber(Range(iRange).Value) And Range(iRange).Value > 0 Then
    tmp = Range(iRange).Value
Else
    tmp = 100
End If

Test = tmp

End Function

I am passing a cell as argument and checking if the cell value is number and >0. But above function showing me value error. What did I do wrong here?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
iRange is a range object, so Range(iRange) is causing an error. Try
Code:
Public Function Test(iRange As Range)

    Dim tmp As Integer

    Set iRange = iRange.Cells(1,1)

    If Application.IsNumber(iRange.Value) And (Val(CStr(iRange.Value > 0))) Then
        tmp = Range(iRange).Value
    Else
        tmp = 100
    End If

    Test = tmp

End Function
The Val(CStr(iRange.Value)) is there to prevent an error if iRange contains an error value, like #DIV/0.
 
Upvote 0
animas,


Rich (BB code):
''Try changing this:
Public Function Test(iRange As Range)

''to this:
Public Function Test(iRange As Range)As Integer
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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