Solved

VBA / Excel/ using optional Parameters

Posted on 2004-09-09
4
696 Views
Last Modified: 2012-06-27
what is wrong with this piece of code, used in VB - Excel application

Function TestOptions(i As Integer, Optional k As Integer) As Integer

  If IsMissing(k) Then
             TestOptions = i
           Else
              TestOptions = i * k
           End If
End Function


Function TestOptionsStr(i As String, Optional k As String) As String

  If IsMissing(k) Then
             TestOptions = "one arg"
           Else
              TestOptions = "Two args"
           End If
End Function
0
Comment
Question by:BdLm
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 50 total points
ID: 12018296
IsMissing works only for variant parameters.
try this
Function TestOptions(i As Integer, Optional k) As Integer

or with default value

Function TestOptions(i As Integer, Optional k As Integer=0) As Integer

  If k=0 Then
             TestOptions = i
           Else
              TestOptions = i * k
           End If
End Function
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12018322
MSDN:

Use the IsMissing function to detect whether or not optional Variant arguments have been provided in calling a procedure. IsMissing returns True if no value has been passed for the specified argument; otherwise, it returns False. If IsMissing returns True for an argument, use of the missing argument in other code may cause a user-defined error. If IsMissing is used on a ParamArray argument, it always returns False. To detect an empty ParamArray, test to see if thearray’s upper bound is less than its lower bound.

Note   IsMissing does not work on simple data types (such as Integer or Double) because, unlike Variants, they don't have a provision for a "missing" flag bit. Because of this, the syntax for typed optional arguments allows you to specify a default value. If the argument is omitted when the procedure is called, then the argument will have this default value, as in the example below:
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12018330
Here is the example that should be in my prev post

Sub MySub(Optional MyVar As String = "specialvalue")
    If MyVar = "specialvalue" Then
       ' MyVar was omitted.
    Else
    ...
End Sub
0
 
LVL 8

Author Comment

by:BdLm
ID: 12063338
thanks for the help, now my pgm is running .-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now