Assign empty string to numeric variable

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I have a form in which I assign some values

Code:
With Me
    Let lBudgetID = .txtBudgetID.Value
    Let lMonthNo = .txtMonth.Value
End With

but if the fields txtBudgetID and txtMonth are empty, the code will fail since it's not possible to convert empty strings to numeric variables.

How can I make sure the code will work?
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You could use Val which will return 0 for an empty string, or you could simply verify the values:
Code:
With Me
    lBudgetID = IIf(.txtBudgetID.Value = "", 0, .txtBudgetID.Value)
    lMonthNo = IIf(.txtMonth.Value = "", 0, .txtMonth.Value
End With
 
Upvote 0

Forum statistics

Threads
1,223,767
Messages
6,174,395
Members
452,561
Latest member
amir5104

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