VBA Loop

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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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