Consulting

Results 1 to 7 of 7

Thread: Solved: Days Difference between text box

  1. #1
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location

    Solved: Days Difference between text box

    Hi there,
    I have two 2 textbox for input of dates. On the 3rd textbox I would like to show no of days between these 2 textbox.Any VBA to automate this

    Helps is appreciated.

    Thank you

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Assuming they are on the worksheet, you could try this in the workshet code module:
    [vba]
    Private Sub TextBox3_GotFocus()
    Me.TextBox3.Value = CDate(Me.TextBox2.Value) - CDate(Me.TextBox1.Value) & " Days"
    End Sub

    [/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    hey tlchan
    or possibly the excel "DateDiff" Function

    [VBA]
    Private Sub TextBox3_GotFocus()
    Me.TextBox3.Value = DateDiff("yyyy",Me.TextBox2.Value,Me.TextBox1.Value)
    End Sub
    [/VBA]
    pike

  4. #4
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location
    Hi Pike & Simon,
    Thanks for your quick solution. Bothe codes work but the result showed in -ve value. How to overcome this?

    Need your help again.

  5. #5
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    Hi tichan
    try
     Me.TextBox3.Value = DateDiff("yyyy",Me.TextBox1.Value,Me.TextBox2.Value)
    pike

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    IF either date could be the later, you could use

    [vba]

    Private Sub TextBox3_GotFocus()
    Me.TextBox3.Value = Abs(CDate(Me.TextBox2.Value) - CDate(Me.TextBox1.Value) & " Days")
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location
    Hi there,

    Great thanks for the quick solotions and option. I really appreciated all your assistance.

    Thanks

Posting Permissions

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