Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel: Difference between 2 ComboBox Dates 1

Status
Not open for further replies.

MeGustaXL

Technical User
Aug 6, 2003
1,055
GB
Hello All

Among the 20 or so controls on a UserForm I have 2 ComboBoxes: cboDateON and cboDateOFF, and a TextBox txtDaysFitted.

the cbo's are populated at Initialise() from a list of dates like this:

01 Jan 03
02 Jan 03
03 Jan 03
04 Jan 03
05 Jan 03
06 Jan 03
07 Jan 03
.........etc.
If that list is named "Date", I've done this:

With cboDateON
.RowSource = [Date].Address
.ListIndex = 0
End With
With cboDateOFF
.RowSource = [Date].Address
.ListIndex = 0
End With


What I'd like to do is a kind of DATEDIF thing to populate txtDaysFitted, something like this:

Private Sub cboDateOFF_Change()
txtDaysFitted.Value = (cboDateOFF.Value) - (cboDateON.Value)
End Sub


Everything I've tried so far, including CDate(cboDateON) returns an error of some sort, usually "Type Mismatch".

Can you give me a clue as to the correct way of doing this calculation?

P.S. I'm still working on a way to only do it when there's a value in both cbo's! [wink]

Chris

 
I am assuming that Excel (or VBA) is interpreting the dates in the COmboBoxes as Text, so try this:

Code:
Private Sub cboDateOFF_Change()
txtDaysFitted.Value = DateValue(cboDateOFF) - DateValue(cboDateON)
End Sub
/color]

I hope this helps!


Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Chris,

don't add the /color] to the end of your code! ;-)

I was a little too excited and pressed "Submit Post" before I proof-read it.



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Nope, Sorry Mike, that gives me a Type Mismatch as well [sad]

I should mention that although the cbo's display the dates as printed above, once you select one, the value displayed is a number like 35678. That's why I thought I could just subtract one from the other.

BTW, where do you guys learn all this stuff - DateValue???

Chris
 
Hi,

Actually, VBA is probably reading the dates as dates in your combobox
Code:
txtDaysFitted.Text = cboDateOFF.Value - cboDateON.Value
Use your Watch Window to observe the value of cboDateOFF. you can learn alot!

;-)

Skip,
Skip@TheOfficeExperts.com
 
Cowabunga, Skip! That's fixed it nicely [2thumbsup]

I really can't wait until the postman delivers my copy of JW's book "XL 2000 Bible"; then I'll (maybe) remember the difference between .Value and .Text [wink]

BTW: The Watch Window didn't help in this case, it just says "Out of Context" [sad]

BTW2: It kinda blows that the dates are displayed as numbers when they're selected, but look like "dd mmm yy" in the drop-down list; any clues as to how/where to apply the formatting? (sheepish grin and fluttering eyelashes)

Chris
 
Yep, got it now. Thanks again Skip!

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top