Looping an Inputbox.

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

I need to loop an Input box until the User answers Yes

I ask for a year, and once the input is received I ask the year that has been entered is correct.

If the answer is Yes, then carry on with code, if the answer is No, then I want it to loop back to where is asks for the year again.

The code I have is :-

VBA Code:
Dim myYear As Integer
Dim Confirm As Integer
'Populate Cell R2 in Sheet named Forumula with the YYYY that will be entered by the User.

myYear = InputBox("Please enter the year (Format YYYY) that you want to create monthly Till Taking Sheets for and other associated files.", Title:=" Company Name")
Sheets("Formula").Select
Range("R2").Value = myYear
Confirm = MsgBox("The Year you want to create files for is " & myYear & " Is that correct?", vbQuestion + vbYesNo + vbDefaultButton2, Title:=" Company Name")
If Confirm = vbYes Then
     MsgBox "The Year you want to create files for is " & myYear & " Is that correct?", vbQuestion + vbYesNo + vbDefaultButton2, Title:=" Company Name "
Else
[COLOR=rgb(0, 0, 0)][B]     Don’t know how to get back to the above InputBox[/B][/COLOR]
End If
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How about
VBA Code:
Dim myYear As Variant
Dim Confirm As String
'Populate Cell R2 in Sheet named Forumula with the YYYY that will be entered by the User.

Do
   myYear = InputBox("Please enter the year (Format YYYY) that you want to create monthly Till Taking Sheets for and other associated files.", Title:=" Company Name")
   If myYear = "" Then Exit Sub
   Confirm = MsgBox("The Year you want to create files for is " & myYear & " Is that correct?", vbQuestion + vbYesNo + vbDefaultButton2, Title:=" Company Name")
Loop Until Confirm = vbYes
Sheets("Formula").Select
Range("R2").Value = myYear
 
Upvote 0
Fluff,

Will try it out tomorrow and let you know (not doubting what you have provided).
 
Upvote 0
Fluff,

I have tried out your revised code and as expected, it works like a charm. :):):)

What would I do if I wanted to ensure that the year entered is either the current year or in the future?

If the Year entered is in the past, or the User has just selected OK in the InputBox, then display the message “Year in the past or Invalid Year entered”

Loop until a valid year has been entered.
 
Upvote 0
You need to give the user a chance to escape, otherwise they are stuck in a permanent loop with no way out.
To check for a valid date use
VBA Code:
Sub Kayslover()
Dim myYear As Variant
Dim Confirm As String
Dim AllOk As Boolean
'Populate Cell R2 in Sheet named Forumula with the YYYY that will be entered by the User.

Do Until AllOk
   myYear = InputBox("Please enter the year (Format YYYY) that you want to create monthly Till Taking Sheets for and other associated files.", Title:=" Company Name")
   If myYear = "" Then Exit Sub
   If Val(myYear) >= Year(Date) Then
      Confirm = MsgBox("The Year you want to create files for is " & myYear & " Is that correct?", vbQuestion + vbYesNo + vbDefaultButton2, Title:=" Company Name")
      If Confirm = vbYes Then AllOk = True
   Else
      MsgBox "please enter a vaild date"
   End If
Loop
Sheets("Formula").Select
Range("R2").Value = myYear
End Sub
 
Upvote 0
Solution
Fluff,

Fantastic, works great.?

Once again I appreciated your assistance.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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