default value for sub

amiroo

Board Regular
Joined
Dec 24, 2013
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
Hi
I have a form. i need run a sub with textbox value as default values. something like this:

Code:
Private Sub Submit_1_Click(Optional serial = TSerial_1.Value, Optional model = CModel_1.Value)

but when I run it, an error occurred that you should use constant value. is there any way to fix it?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi

Just use some constant value that you know it's not a valid parameter value and then test inside, like:


Code:
Private Sub Submit_1_Click(Optional serial = "Optional", Optional model = "Optional")

If serial = "Optional" Then serial = TSerial_1.Value
If model = "Optional" Then model = CModel_1.Value

' ...
 
Upvote 0
Hi

Just use some constant value that you know it's not a valid parameter value and then test inside, like:


Code:
Private Sub Submit_1_Click(Optional serial = "Optional", Optional model = "Optional")

If serial = "Optional" Then serial = TSerial_1.Value
If model = "Optional" Then model = CModel_1.Value

' ...

thanks. so there is no way to use a dynamic value as input?
 
Upvote 0
thanks. so there is no way to use a dynamic value as input?

As far as I know, not directly in the procedure header like you wanted.


By the way, the answer I posted works but is dumb, you don't need to specify any value for the optional parameters.

I should have posted:

Code:
Private Sub Submit_1_Click(Optional serial, Optional model)

If IsMissing(serial) Then serial = TSerial_1.Value
If IsMissing(model) Then model = CModel_1.Value

' ...

As you can see vba already provides you with IsMissing() that allows you to assign a dynamic value to the parameters,
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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