Default Values for Parameters in a Sub

Posted on 2004-04-15
Medium Priority
Last Modified: 2010-05-02
Hello Experts!

Can't really believe i'm asking this question cos it seems so obvious, but for some reason i cant find the correct syntax!

How do i set a default for a parameter in a sub?


Private Sub Example(MyVariable as Integer, MySecondVariable as Integer)

and i want to put
Example , 2

If no Parameter is entered, how would i set MyVariable to 1 for example?

At the moment i'm just getting an error saying argument not optional if i leave it blank!
Cheers in advance,

Question by:Bobeo
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
  • 2
LVL 48

Expert Comment

ID: 10836337
Private Sub Example(MyVariable as Integer,optional  MySecondVariable as Integer = 2)
LVL 48

Accepted Solution

Mikal613 earned 200 total points
ID: 10836345
Private Sub Example(MyVariable as Integer,optional  MySecondVariable as Integer = 1)
LVL 19

Expert Comment

ID: 10836372
Private Sub Example(Optional MyVariable As Integer = 1, Optional MySecondVariable As Integer = 1)



Author Comment

ID: 10836405
Thanks Mikal613, just what i needed!

Featured Post

Industry Leaders: 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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