PDA

View Full Version : Solved: Set Msgbox with Default number?



doctortt
03-27-2011, 11:36 PM
Hi,

I'm new to VBA programming.

How do you write the codes for showing an inputbox asking the user to enter a number, but if the user leaves the box blank, Excel will default it to 35? Can someone help?

I only have these so far:

Dim Age as Double
Age = InputBox("Please Enter Your Age" & vbCrLf & "Default: 35")

mikerickson
03-28-2011, 12:02 AM
Welcome to VBA Express.

One of the forum practices (rules) here is wrapping all code, even snippets like the one above. I fixed it this time.

This uses Application.InputBox and the Type argument to validate the user's entry.
Dim Age As Double

Age = Application.InputBox("Enter your age.", Default:=35, Type:=1)

If Age = 0 Then
MsgBox "Cancel pressed"
Else
MsgBox Age & " entered."
End If

doctortt
03-28-2011, 12:09 AM
It works. Thanks