ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Subtracting Dates in TextBoxes (https://www.excelbanter.com/excel-programming/312200-subtracting-dates-textboxes.html)

John Wilson

Subtracting Dates in TextBoxes
 
Anyone have an idea how to do the following:

On a UserForm.
TextBox1 = 9/20/04
TextBox2 = 9/28/04

Once the date is entered into TextBox2, TextBox 3 should populate with:
"1 week & 2 days"

Thanks,
John



Doug Glancy

Subtracting Dates in TextBoxes
 
John,

Here's a start. It doesn't check to make sure date1 is earlier than date2.
It also starts filling in box3 before the second date is completely entered
because I used a Change event. An Exit event would only work if you left
one of the textboxes which seems worse to me. Anyways, enter this code into
the UserForm, or just wait for one of the many really smart people in this
group to follow up with a more complete answer:

Private Sub TextBox1_Change()
Call date_change
End Sub

Private Sub TextBox2_Change()
Call date_change
End Sub

Sub date_change()
Dim dif As Long

If IsDate(Me.TextBox1) And IsDate(Me.TextBox2) Then
dif = CDate(Me.TextBox2) - CDate(Me.TextBox1) + 1
Me.TextBox3 = Int((dif) / 7) & " weeks " _
& dif Mod 7 & " days"
End If
End Sub

hth,

Doug Glancy

"John Wilson" wrote in message
...
Anyone have an idea how to do the following:

On a UserForm.
TextBox1 = 9/20/04
TextBox2 = 9/28/04

Once the date is entered into TextBox2, TextBox 3 should populate with:
"1 week & 2 days"

Thanks,
John





Bob Phillips[_6_]

Subtracting Dates in TextBoxes
 
Textbox3.Text = CDate(Textbox2.Text) - CDate(Textbox1.Text)

will give you 8. You can work out how to turn that into weeks and days,
especially as you get 9?

--

HTH

RP

"John Wilson" wrote in message
...
Anyone have an idea how to do the following:

On a UserForm.
TextBox1 = 9/20/04
TextBox2 = 9/28/04

Once the date is entered into TextBox2, TextBox 3 should populate with:
"1 week & 2 days"

Thanks,
John





John Wilson

Subtracting Dates in TextBoxes
 
Thanks guys.....

CDate was the "key" that I was missing.
Brain fart, I guess?

Thanks again,
John

"Bob Phillips" wrote in message
...
Textbox3.Text = CDate(Textbox2.Text) - CDate(Textbox1.Text)

will give you 8. You can work out how to turn that into weeks and days,
especially as you get 9?

--

HTH

RP

"John Wilson" wrote in message
...
Anyone have an idea how to do the following:

On a UserForm.
TextBox1 = 9/20/04
TextBox2 = 9/28/04

Once the date is entered into TextBox2, TextBox 3 should populate with:
"1 week & 2 days"

Thanks,
John








All times are GMT +1. The time now is 07:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com