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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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