dumbitdown

New Member
Joined
Jul 23, 2007
Messages
28
Afternoon all,

Looking for a bit of help with looping...

Basically I've created a macro that gets the user to enter data via input boxes and validates the info they have entered, when the format of the info is incorrect I have an error message pop up, when ok is clicked on this error message I want it to loop back round to get them to re-enter the data in the correct format.

I've extracted a bit of the macro to demonstrate (below), how do I make it loop back round to get them to try and enter the data correctly?

Dim strDate As String
strDate = InputBox("Target Date dd/mm/yyyy", "User date", Format(Now(), "dd/mm/yyyy"))
If IsDate(strDate) Then
strDate = Format(CDate(strDate), "dd/mm/yyyy")
MsgBox strDate
Else
MsgBox "Wrong date format"

Any help you can give would be brilliant!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Perhaps

Code:
Dim strDate As String
Do Until IsDate(strDate)
    strDate = InputBox("Target Date dd/mm/yyyy", "User date", Format(Now(), "dd/mm/yyyy"))
    If IsDate(strDate) Then
        strDate = Format(CDate(strDate), "dd/mm/yyyy")
        MsgBox strDate
    Else
        MsgBox "Wrong date format"
    End If
Loop
 
Upvote 0
This works perfectly for me

Code:
Sub atest()
Dim strDate As String
Do Until IsDate(strDate)
    strDate = InputBox("Target Date dd/mm/yyyy", "User date", Format(Now(), "dd/mm/yyyy"))
    If IsDate(strDate) Then
        strDate = Format(CDate(strDate), "dd/mm/yyyy")
        MsgBox strDate
    Else
        MsgBox "Wrong date format"
    End If
Loop
End Sub
 
Upvote 0
Thanks for your help, I have just noticed an issue with my own code, I want it to insert the date into the active cell however it's validating the data and making the date pop up in a message box - could you help me make it validate the date and insert it into the active cell? (hope that makes sense!) Thanks
 
Upvote 0
Try

Code:
Sub atest()
Dim strDate As String
Do Until IsDate(strDate)
    strDate = InputBox("Target Date dd/mm/yyyy", "User date", Format(Now(), "dd/mm/yyyy"))
    If IsDate(strDate) Then
        ActiveCell.Value = DateValue(strDate)
    Else
        MsgBox "Wrong date format"
    End If
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,752
Messages
6,132,512
Members
449,731
Latest member
dasda34

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