VBA query: Create error if second date is before first.

K1600

Board Regular
Joined
Oct 20, 2017
Messages
181
I have the following code in a larger piece of coding which basically runs a report between two selected dates. I want to keep the current setup but also create an error if the second date (ans2) is before the first (ans1). I don't mind if I have to create a combined error message for an invalid date and also dates in the wrong order if that makes it easier to resolve.

Thanks in advance.

VBA Code:
Private Sub CmdRunReport_Click()
    
    Dim wbk1 As Workbook
    Dim sht1 As Worksheet
    Dim wbk2 As Workbook
    Dim sht2 As Worksheet
    Dim startdate As Date, enddate As Date
    Dim rng As Range, destRow As Long
    Dim c As Range
    Dim ans1 As String, ans2 As String
  

Application.ScreenUpdating = False  'Stops screen showing during process

'Gets input boxes for to/from dates for report
1 ans1 = InputBox("Enter Start Date", "Admin Dashboard", "dd/mm/yyyy")
    If ans1 = "" Then
        Exit Sub
    ElseIf IsDate(ans1) Then
        startdate = CDate(ans1)
    Else
    MsgBox "Please enter a valid start date"
    GoTo 1
    End If

2 ans2 = InputBox("Enter End Date", "Admin Dashboard", "dd/mm/yyyy")
    If ans2 = "" Then
        Exit Sub
    ElseIf IsDate(ans2) Then
        enddate = CDate(ans2)
    Else
    MsgBox "Please enter a valid end date"
    GoTo 2
    End If

'The rest of the coding for running the report follows from here......
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Happy Easter!

Add this just above your comment specifying that
VBA Code:
'The rest of the coding for running the report follows from here......

VBA Code:
If enddate < startdate Then
MsgBox "The ending date must be greater that the starting date.  Your starting date was: " & startdate
GoTo 2
Else
End If
 
Upvote 0
Absolutely spot on COwen. I knew it would be something simple.

Thank you kindly and Happy Easter to you too.
 
Upvote 0
Glad I could help! Thank you for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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