Solved

VBA / Excel/ using optional Parameters

Posted on 2004-09-09
4
716 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

856 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