VBA Input Box Data Restrictions

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
Folks:

My code in progress but is there a better way to force the user to input the correct month?:

Code:
[/COLOR]
[COLOR=black]Sub INPUT_NUMBER_FROM_1_THRU_12()[/COLOR]
[COLOR=black]Dim myNum As Long[/COLOR]
[COLOR=black]myNum = Application.InputBox("Enter Month # from 1-12", "CURRENT MONTH")[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]If myNum < 1 Or myNum > 12 Then[/COLOR]
[COLOR=black]   myNum = Application.InputBox("You Must Enter Month # from 1-12", "CURRENT MONTH")[/COLOR]
[COLOR=black]     If myNum < 1 Or myNum > 12 Then[/COLOR]
[COLOR=black]       myNum = 1[/COLOR]
[COLOR=black]       MsgBox "INCORRECT INPUT SO DEFAULT TO  " & myNum[/COLOR]
[COLOR=black]       GoTo TakeItHome[/COLOR]
[COLOR=black]     Else[/COLOR]
[COLOR=black]       GoTo NoProblem[/COLOR]
[COLOR=black]     End If[/COLOR]
[COLOR=black]Else[/COLOR]
[COLOR=black]   GoTo NoProblem[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]NoProblem:[/COLOR]
[COLOR=black]   MsgBox "You typed  " & myNum & ",  That is within the acceptable range"[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]TakeItHome:[/COLOR]
[COLOR=black]'Application.InputBox (Prompt, Title, Default, Left, Top, HelpFile, HelpContextId, Type)[/COLOR]
[COLOR=black]'Application.InputBox ("Enter Month # from 1-12", "CURRENT MONTH", "12", Left, Top, HelpFile, HelpContextId, Type)[/COLOR]
[COLOR=black]End Sub[/COLOR]
[COLOR=black]
I may also want to force them to pick a valid month or they can't go on.
 
Last edited:
Probably need default as previous month number instead of current.
Code:
uiMonth = Application.InputBox("Input the month number (1-12)", _
         "LAST COMPLETED MONTH", Default:=[COLOR=darkred]Month(Date) - 1[/COLOR], Type:=1)
Just a follow up question for your consideration... what if the user is running your macro on the last day of the current month, is the previous month still considered the "last completed month" (for the default value to be used in the InputBox)?
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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