Checking the input of a function?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
to all,

rather than writing a "complicated" formula in a cell, I am writing a function. I am trying to understand wha tis the best way of checking if a value as been specified for an input.
I set up my test with IsMissing() but the input must be a Varian. I am working with Double

In my test I wan to make sure that the input has been specified or it's not zero. The zero part works
Code:
Function test1(inputvalue As Double) As String

Dim msg As String

    If inputvalue = 0 Then 'IsMissing(inputvalue) only work for Variant
        Debug.Print ; "value is missing"
        Debug.Print ; inputvalue
        msg = "value is missing"
    Else
        msg = "value is: " & inputvalue
    End If

    MsgBox msg
    test1 = msg

End Function
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You can convert the data type to variant using

Code:
[COLOR=#333333]If [/COLOR][COLOR=#000080][FONT='inherit']CVar([/FONT][/COLOR][COLOR=#333333]inputvalue) = 0[/COLOR]

Type Conversion Functions
 
Last edited:
Upvote 0
You can convert the data type to variant using

Code:
[COLOR=#333333]If [/COLOR][COLOR=#000080][FONT='inherit']CVar([/FONT][/COLOR][COLOR=#333333]inputvalue) = 0[/COLOR]

Type Conversion Functions

Thanks for the suggestion. How does on define the variable then ? As Double?
one still needs to specify zero as an input to work. I would like to catch the case where nothing as been specific or zero. Zero being easy to do. is the "nothing" that I am struggling with
 
Upvote 0
Why test if it's a double? Why not test if it's a number or not and then exclude 0? For your code, what difference is there with an input value of 1 vs 1.0?
 
Upvote 0
Thanks for the suggestion. How does on define the variable then ? As Double?
one still needs to specify zero as an input to work. I would like to catch the case where nothing as been specific or zero. Zero being easy to do. is the "nothing" that I am struggling with

YOu can use something like this if you're trying to check for empty variable etc
Code:
[COLOR=#333333]If  inputvalue=""[/COLOR][COLOR=#333333] Or [/COLOR][COLOR=#333333]inputvalue = 0 [/COLOR][COLOR=#333333]Then[/COLOR]
 
Last edited:
Upvote 0
You are missing the Optional argument in case no argument is provided .

How abouth this :
Code:
If inputvalue = Empty Then
 
Upvote 0
Code:
Function test1(Optional inputvalue As Variant) As String
  If IsMissing(inputvalue) Then
    MsgBox "value is missing"
  Else
    MsgBox "value is: " & inputvalue
  End If
End Function
 
Upvote 0
thanks. 'inputvalue' cannot be optional. user must specified something
The Real function will have multiple inputs one of which will be optional
 
Upvote 0
Note that if you want to test to see if a certain input is valid, you will want to use "Variant", as the user could have input anything, and you will want to capture that.
If you limit the input for your test1 function to "Double", you are already assuming one of the things you want to check for (is it a number?), which kind of defeats the purpose of checking it in the first place!
 
Upvote 0
thanks. 'inputvalue' cannot be optional. user must specified something
The Real function will have multiple inputs one of which will be optional

Reckon I'm missing what you're trying to do, then; 0 is as valid as any other value for a number.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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