Consulting

Results 1 to 9 of 9

Thread: Userform in VBA and DATEDIFF function

  1. #1

    Userform in VBA and DATEDIFF function

    Hi

    I am new to the forum so apologies if I've posted this is the wrong place.

    I am trying to create a userform with a calculated field based on data entered in 2 text boxes, the field I would like calculated is the date difference, I would like the value to be displayed in a user form and then posted, along with the rest of the data in a workbook to update a database.

    Can anyone help?

  2. #2
    Something like this to get the difference.

    Private Sub CommandButton1_Click()
       TextBox3.Value = DateDiff("d", CDate(TextBox1.Value), CDate(TextBox2.Value))
    End Sub

  3. #3
    Will I have to create a command button to run the calculation then?

  4. #4
    No you could get it to calculate when you have finished entering data into Textbox2 if you wanted.

    Private Sub TextBox2_AfterUpdate()
        TextBox3.Value = DateDiff("d", CDate(TextBox1.Value), CDate(TextBox2.Value)) 
    End Sub

  5. #5
    I've got it, thank you! I've been trying to think up a way to do it for a while, I'm new to VBA and decided my firest project would be something fairly complicated!

  6. #6
    Haha no problem, thanks for the feedback.

    Im not an expert with VBA either, my practice is TRYING to help people out in various forums

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    FYI, if you need the number of days between two dates, you can simply subtract one from the other.
    Be as you wish to seem

  8. #8
    Might aswell make use of the Datediff function though, thats what its there for :P

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    True. After all, why write =A1+1 when you can write =DATE(YEAR(A1),MONTH(A1),DAY(A1)+1)
    Be as you wish to seem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •