Msgbox for conflicting dates

  • I am trying to figure out how to make a message box display for a date if it is earlier than another date. These are for "Arrival" and "Departure" dates so I would like a message box to show if the departure date is before the arrival date (think that I'm hosting people, not sending them off :)). The dates are in text boxes on a user form. I only have a little bit of experience with VBA but I really enjoy using the program so I appreciate any help I can get!


    I've tried this code:



    but I keep getting "Compile Error: Variable not defined."


    I created a practice user form where I can get the following to work:


    Code
    [VBA]Option Explicit
    Private Sub CommandButton1_Click()
    If TextBox1.value > TextBox2.value Then
    MsgBox "Number is bigger!"
    
    
    End If
    
    
    End Sub[/VBA]


    which works fine regardless of whether it is in a date format or not.

  • Hello,


    In any UserForm, TextBoxes are holding ... Text ...


    So, when it comes to comparing values, you do need to convert these values into NUMBERS ...


    For example


    Code
    If CDate(TxtDepart.Value) < CDate(TextArrival.Value) Then
        MsgBox "Departure date is before arrival date", vbCritical, "Incompatible Departure Date"
        Exit Sub
    End If


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Still getting the issue of it saying that "Variable is not defined"


    Once you are using Option Explicit ...


    Make sure all your variables are declared ...


    Avoid the confusion of your TextBoxes names : ' TextArrive ' vs ' TextArrival '

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • If I try this:


    It says I get a type mismatch error. Am I setting up my variables wrong? Thank you for helping me with this!

  • You are using the wrong function i.e. CInt should be CDate like so:



    I believe you also had the Exit Sub in the wrong spot.


    HTH


    Robert

  • Glad it is finally sorted out ...


    Thanks for your Thanks ..AND for the Like ...:smile:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!