apologies if this is a basic question, but I am self-teaching myself VBA PowerPoint and would appreciate it if someone could explain why I am getting an overflow error.
I have been trying to get the time my program user spends performing an activity using DateDiff and Now to set the start and end timers. My approach is to get the number of seconds and then work out how many days, minutes, hours and seconds will make up this number.
The below code works, however, whenever I use the Messagebox (Msgbox) function I get an overflow error. For example if you look at the code below everything works, until you uncomment one of my commented MsgBoxes and then the program crashes with a Run-time error '6': Overflow. I note the actual crash occurs on one of the subsequent calculation lines below the uncommented message box. I note the first MsgBox which is uncommented does not crash the computer.
I would be grateful if someone could explain why the computer crashes and second are there any changes to my code required in order to make it reliable to use?
Thank you for any comments!
Sub NewTimer()
Dim TimeStart As Date
Dim TimeEnd As Date
Dim Days As Long
Dim Hours As Long
Dim Hours2 As Long
Dim Hours3 As Long
Dim Minutes As Long
Dim Minutes2 As Long
Dim Seconds As Long
Dim TimePassed As String
Dim Seconds2 As Long
Seconds = Int(DateDiff("s", Now, Now + 1))
MsgBox "The total number of seconds that make up this number is " & Seconds
Days = Int(DateDiff("d", Now, Now + 1))
MsgBox "The total number of days in this number is " & Days
Hours2 = Seconds - (Days * 24 * 60 * 60) 'This gets the number of seconds remaining
Hours3 = Hours2 / (60 * 60) 'This works out the number of hours
Hours = Int(Hours3)
'MsgBox Hours
Minutes = Seconds - (Days * 24 * 60 * 60) - (Hours * 60 * 60) ' Hours2 - (Hours * 60 * 60)
'MsgBox Minutes
Minutes2 = Int(Minutes / 60)
'MsgBox Minutes2
'Minutes2 = Minutes / 60
Seconds = Seconds - (Days * 24 * 60 * 60) - (Hours * 60 * 60) - (Minutes2 * 60)
MsgBox "The time taken is " & Days & " days, " & Hours & " hours, " & Minutes2 & " minutes and " & Seconds & " seconds."
As other comments, when I ran your code I had no issues at all. However, you can achieve the same result with this code. See if it helps:
Sub NewTimer()
Dim TimeStart As Double
Dim TimeEnd As Double
Dim TimeDiff As Double
Dim Days As Long
Dim TimeDiffString As String
TimeStart = Now
TimeEnd = TimeStart + 4 + TimeValue("04:13:25")
TimeDiff = TimeEnd - TimeStart
Days = Int(TimeDiff)
TimeDiffString = Days & " days, " & Hour(TimeDiff) & " hours, " & Minute(TimeDiff) & " minutes and " & Second(TimeDiff) & " seconds."
MsgBox TimeDiffString
End Sub
Edit: follow-up to comments below.
In office applications, you can safely think of dates and times as decimal (floating point) numeric values, where the integer part is the number of days and the fraction part is a fraction of the day. Examples will make this clear:
Office applications give you many functions you can use to format dates/times. For example in VBA:
Hour()
gives you the hour on the clock. Hour(1.27)
= Hour(3.27)
= Hour(295.27)
= 6Minute()
gives you the minutes on the clock. Minute(1.27)
= Minute(3.27)
= Minute(295.21)
= 28Second()
gives you the seconds on the clock. Second(1.27)
= Second(3.27)
= Second(295.21)
= 48This is the same as saying that (6 hours + 28 minutes + 48 seconds) adds up to 0.27 of 1 day.
Day()
gives you the calendar day of the month, where day 1 is defined depending on the context and your settings. For example in Excel VBA day 1 is generally December 31, 1899. therefore, Day(295.21)
= 21 because 295.21 is 294 days after December 31, 1899 which corresponds to October 21, 1900.Month()
gives you the calendar month of the year. Therefore, Month(295.27)
= 10Year()
gives you the calendar year. Therefore, Year(295.27
= 1900This explains why I separated the day (the integer part of DateDiff
in the code). If for example DateDiff
= 295.27, then Day(DateDiff)
is going to give you 21 not the 295 days you expect.
Homework: in the code above work out what fraction TimeValue("04:13:25")
should result in. Check your answer with this code - no cheating ;):
Sub CheckMyAnswer()
MsgBox Format(TimeValue("04:13:25"), "0.000000")
End Sub
Phew, hope this helps. I am sure if you google the issue you will find far better explanations and plenty of videos, too.