F1990
F1990

Reputation: 637

Calculation with seconds vba

I have a column in excel that stores the difference between and end date and start date in seconds.

Time window seconds
2580 (0:43)
16200

I would like to sum these seconds and store this information in an array. This array should then contain the totalnumber of seconds. However if I specify this array as:

Dim totalnumber(7) as Date

I get weird values in Date format, instead of seconds. The next step is to do a simple calculation with each number stored in the array. How should I define this array so that it contains seconds and that I can do a calculation with it?

Upvotes: 0

Views: 170

Answers (2)

LimaNightHawk
LimaNightHawk

Reputation: 7093

If it's a whole number representing the number of seconds, I would use an array of Long.

Dim seconds(7) as Long

PROTIP: You can use the DateAdd and DateDiff functions to calculate Dates and portions of Dates with and from whole numbers. E.g.

Dim lSeconds as Long
lSeconds = DateDiff("s", date1, date2)

Dim dtNew as Date
dtNew = DateAdd("s", lSeconds, date1)

See here:

DateDiff

DateAdd

Upvotes: 2

Stadem
Stadem

Reputation: 423

When you calculate the difference between two days, the Date format gives you a value that is expressed in days. Digits after the decimal point give you the hours, minutes, seconds, milliseconds, etc. For example, the Date value 2.5 would represent 2 days, 12 hours (12 hours = 0.5 days).

To convert a Date value to seconds, multiply by 86,400 (86,400 seconds in a day).

Upvotes: 0

Related Questions