ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Here's an Easy One (https://www.excelbanter.com/excel-programming/374026-heres-easy-one.html)

Kevin

Here's an Easy One
 
I have the following two variables:

dim myVar as single
dim myDate as date

myVar ends up with a value of 0.96.
myDate ends up with a value of "9/29/06 12:33".

How do I add myVar to myDate to get "9/30/06 11:37"?

I've tried running every conversion function on myVar I can think of and
I'm still getting a type mismatch.

-Kevin

Kevin

Here's an Easy One
 
Forgot to mention. I'm trying to store the result in this variable:

dim myResult as date

semiopen

Here's an Easy One
 

Kevin wrote:
Forgot to mention. I'm trying to store the result in this variable:

dim myResult as date


Try this:

Sub test()

Dim myVar As Single
Dim myDate As Date
Dim myResult As Date
Dim myMinutes As Long


myVar = 0.96
myMinutes = Round(myVar * 24 * 60) 'myVar times # of minutes in a day
myDate = "9/29/06 12:33"
myResult = DateAdd("n", myMinutes, myDate)
MsgBox myResult

End Sub

-------------------------------------

I don't know if there is a more elegant way

-semiopen


Tom Ogilvy

Here's an Easy One
 
Not sure where you get 37,

Sub EFG()
Dim myVar As Double
Dim myDate As Date
Dim myDate1 as Date

myVar = 0.96
myDate = DateValue("9/29/06") + TimeValue("12:33")
myDate1 = myDate + myVar

Debug.Print myDate1

End Sub
returns
09/30/2006 11:35:24 AM

which appears correct since

? format(.96,"hh:mm:ss")
23:02:24

--
Regards,
Tom Ogilvy


"Kevin" wrote:

Forgot to mention. I'm trying to store the result in this variable:

dim myResult as date


Kevin

Here's an Easy One
 
Try this:

Sub test()

Dim myVar As Single
Dim myDate As Date
Dim myResult As Date
Dim myMinutes As Long


myVar = 0.96
myMinutes = Round(myVar * 24 * 60) 'myVar times # of minutes in a day
myDate = "9/29/06 12:33"
myResult = DateAdd("n", myMinutes, myDate)
MsgBox myResult

End Sub

-------------------------------------

I don't know if there is a more elegant way

-semiopen


Worked like a charm. Never knew about the dateadd function. Things
like timevalue, datevalue, and cdate are all over the place. Thank you!

-Kevin

moon[_7_]

Here's an Easy One
 

Or this...

Sub Test()
Dim myVar As Single
Dim myDate As Date
myVar = 0.96
myDate = "9/29/06 12:33"
MsgBox myDate + myVar
End Sub




"Kevin" schreef in bericht
...
Try this:

Sub test()

Dim myVar As Single
Dim myDate As Date
Dim myResult As Date
Dim myMinutes As Long


myVar = 0.96
myMinutes = Round(myVar * 24 * 60) 'myVar times # of minutes in a day
myDate = "9/29/06 12:33"
myResult = DateAdd("n", myMinutes, myDate)
MsgBox myResult

End Sub

-------------------------------------

I don't know if there is a more elegant way

-semiopen


Worked like a charm. Never knew about the dateadd function. Things like
timevalue, datevalue, and cdate are all over the place. Thank you!

-Kevin




semiopen

Here's an Easy One
 

moon wrote:
Or this...

Sub Test()
Dim myVar As Single
Dim myDate As Date
myVar = 0.96
myDate = "9/29/06 12:33"
MsgBox myDate + myVar
End Sub


I didn't know it was that simple. I assumed that the OP was getting a
type-mismatch error (else why all this concern about type conversion?)
- so I typed "date" in Help and saw the DateAdd function and thought
"bingo". I seldom play around with dates in my code - learn something
new every day.

-semiopen


"Kevin" schreef in bericht
...
Try this:

Sub test()

Dim myVar As Single
Dim myDate As Date
Dim myResult As Date
Dim myMinutes As Long


myVar = 0.96
myMinutes = Round(myVar * 24 * 60) 'myVar times # of minutes in a day
myDate = "9/29/06 12:33"
myResult = DateAdd("n", myMinutes, myDate)
MsgBox myResult

End Sub

-------------------------------------

I don't know if there is a more elegant way

-semiopen


Worked like a charm. Never knew about the dateadd function. Things like
timevalue, datevalue, and cdate are all over the place. Thank you!

-Kevin




All times are GMT +1. The time now is 06:35 AM.

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