Solved

VBA / Excel/ using optional Parameters

Posted on 2004-09-09
4
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

705 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