424,996 Members | 896 Online
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

# Overflow? What gives?

 P: n/a I'm puzzled. The following code is designed to take a date and a weekday count value, and return the future date. For example, if I supply it a starting date of 4/25/2007 and a count of 37, it should return a date that is 37 weekdays only from 4/25/2007. It should not count any weekends, only Monday-Friday days. When I ran this code, it came back with an OVERFLOW error. Huh? Function ForwardDate(ByVal EndDate As Date, _ ByVal DaysForward As Double) As Date Dim RoundedDays As Long Dim x As Long Dim CalendarDays As Long Dim Weeks As Integer Dim Remainder As Single Dim TimeDay As Date RoundedDays = CInt(DaysForward) Weeks = Fix(RoundedDays / 5) 'trading day weeks Remainder = RoundedDays Mod 5 'days left over '***** THIS IS THE LINE THAT GIVES THE ERROR ***** 'when Weeks = 5731. That comes out to 40117 which should be within 'the LONG range of CalendarDays. CalendarDays = Weeks * 7 'convert trading day weeks to calendar days TimeDay = EndDate + CalendarDays 'expand out by calendardays. should fall on same weekday 'Now add the remainder days excluding weekends For x = 1 To Remainder TimeDay = TimeDay + 1 If Weekday(TimeDay) = vbSaturday Then: TimeDay = TimeDay + 2 'shift from Saturday to Monday Next x ForwardDate = TimeDay End Function ====================== Program stops with an Overflow error on the line CalendarDays = Weeks * 7. Looking at Weeks, it was equalling 5731. That is within range for an INT. And when multiplied by 7, that should be within range of a LONG. Can anyone see what I did wrong here? Thanks. Webbiz Apr 24 '07 #1
7 Replies

 P: n/a Dim Weeks As Integer > '***** THIS IS THE LINE THAT GIVES THE ERROR ***** 'when Weeks = 5731. That comes out to 40117 which should be within 'the LONG range of CalendarDays. CalendarDays = Weeks * 7 'convert trading day weeks to calendar days Because Weeks is declared as an Integer and 7 can fit into an Integer, VB stores the intermediate calculation in as Integer, or at least it tries to... 40117 is too big to fit in an Integer, so you get an overflow error. There is almost no reason to declare variables as Integer (Longs are faster in a 32-bit world), so I would simply declare Weeks as Long, which should take care of your problem (as long as one of the values is bigger than an Integer, VB will not perform the intermediate calculations as Integer, even if the other value is an Integer). If you need Weeks to be an Integer for other reasons, then simply change 7 to 7& (which make 7 a Long value); or change the 7 to CLng(7) which has the same effect. Rick Apr 24 '07 #2

 P: n/a Thanks Rick. After some thought, I thought that maybe this was the case, that VB was 'temporarily' storing the result in Weeks thus causing the overflow. So I had changed it to Long type. Thanks for the advice to do so for all my Ints in the future. Regards, Webbiz "Rick Rothstein (MVP - VB)" Dim Weeks As Integer '***** THIS IS THE LINE THAT GIVES THE ERROR ***** 'when Weeks = 5731. That comes out to 40117 which should be within 'the LONG range of CalendarDays. CalendarDays = Weeks * 7 'convert trading day weeks to calendar days Because Weeks is declared as an Integer and 7 can fit into an Integer, VB stores the intermediate calculation in as Integer, or at least it tries to... 40117 is too big to fit in an Integer, so you get an overflow error. There is almost no reason to declare variables as Integer (Longs are faster in a 32-bit world), so I would simply declare Weeks as Long, which should take care of your problem (as long as one of the values is bigger than an Integer, VB will not perform the intermediate calculations as Integer, even if the other value is an Integer). If you need Weeks to be an Integer for other reasons, then simply change 7 to 7& (which make 7 a Long value); or change the 7 to CLng(7) which has the same effect. Rick Apr 24 '07 #4

 P: n/a Sorry about that. I had lost all my newsgroups and wasn't sure which of the two VB newsgroups was the one I used to participate in months back. I tried to find names I recognized (like yours) and didn't see any in the initial list that came up when I clicked on the newsgroup name. In addition, I noted some 'not so nice' language used on one of the two and figured it may not be the same newsgroup I participated in before. But I wasn't sure. So I posted in both and thought to see which is actually the one that has good participation. Webbiz "Rick Rothstein (MVP - VB)"

 P: n/a I had lost all my newsgroups and wasn't sure which of the two VB newsgroups was the one I used to participate in months back. I tried to find names I recognized (like yours) and didn't see any in the initial list that came up when I clicked on the newsgroup name. You should consider using Microsoft's public newsgroups; specifically, set up an account for msnews.microsoft.com and select from among the newsgroups with 'vb' in their names. Most activity seems to occur in this one... microsoft.public.vb.general.discussion Rick Apr 24 '07 #6

 P: n/a >I had lost all my newsgroups and wasn't sure which of the two VB >newsgroups was the one I used to participate in months back. I tried tofind names I recognized (like yours) and didn't see any in the initiallist that came up when I clicked on the newsgroup name. You should consider using Microsoft's public newsgroups; specifically, set up an account for msnews.microsoft.com and select from among the newsgroups with 'vb' in their names. Most activity seems to occur in this one... microsoft.public.vb.general.discussion But if you want to stay in the comp.lang newsgroups, I would suggest using comp.lang.basic.visual.misc which seems to get more activity than the two you selected. Rick Apr 25 '07 #7

 P: n/a Thanks again Rick. Will do! "Rick Rothstein (MVP - VB)" I had lost all my newsgroups and wasn't sure which of the two VBnewsgroups was the one I used to participate in months back. I tried tofind names I recognized (like yours) and didn't see any in the initiallist that came up when I clicked on the newsgroup name. You should consider using Microsoft's public newsgroups; specifically, set up an account for msnews.microsoft.com and select from among the newsgroups with 'vb' in their names. Most activity seems to occur in this one... microsoft.public.vb.general.discussion Rick Apr 27 '07 #8

### This discussion thread is closed

Replies have been disabled for this discussion.