LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Incrementing dates by one hour returns bad result

I am currently attempting to write code that will take
input from the user as to the start and stop date of an
event of interest, then generate a range of dates by hour
(e.g. 1/1/03 12:00 AM, then 1/1/03 1:00 AM, etc.) between
the specified dates. These will be used to vlookup y
values corresponding to the dates on another sheet. I am
having what seems to me to be a strange problem and did
not find any information on it in net searches or by
searching this group.

The following is the portion of the code relating to the
problem:

Storing the start date information from the user:

CommandButton1_Click_EnterEvent:
Dim StartDate As Date
StartDate = DateValue(InputBox("Enter event start
date:", "Event Start Date"))

Later in the code I use a do loop to generate the dates
and print them to a worksheet:

Dim Counter As Integer
Dim dt As Date
dt = StartDate + TimeValue("12:00 AM")
Counter = 8
ActiveSheet.Cells(Counter, 2) = dt
Do While dt < StopDate
dt = dt + TimeValue("01:00:00")
Counter = Counter + 1
ActiveSheet.Cells(Counter, 2) = dt
Loop

If I use a date increment of one minute (e.g. dt = dt +
DateAdd("n" , 1 , dt), this works. If I use an increment
of two hours or one day it works. But if I use an
increment of one hour (done as shown above, or with
DateAdd, or by simply saying "dt = dt + (1 / 24)," the
dates fill in successfully until 11:00 PM--but then the
next line is midnight the same day instead of midnight the
next day.

Example output in worksheet column:

5/1/2003, 5/1/2003 1:00, 5/1/2003 2:00, ... ,5/1/2003
23:00, 5/1/2003 0:00 (not 5/2/2003 0:00 as it should be).

If I check this by dragging the first few dates over to
the next column and autofilling them manually, the dates
proceed to 5/2/2003 0:00 as they should. Also, the
first "5/1/2003" does indeed represent 5/1/2003 0:00
because the dates are used in a lookup table--and the
first "5/1/2003" returns the same lookup value as the
second "5/1/2003 0:00."

I am at the end of my rope with this one. Thanks in
advance for your time and assistance.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Round result of sum function to quarter hour Trudy Excel Discussion (Misc queries) 3 March 4th 08 10:17 PM
Incrementing dates in the format MMM-YY kmewing Excel Discussion (Misc queries) 3 October 19th 07 06:03 PM
Repeating/incrementing dates 35039 times Daiv Excel Discussion (Misc queries) 5 January 4th 07 09:54 PM
result returns #N/B Léon Excel Discussion (Misc queries) 4 February 9th 06 04:44 PM
vlookup based on random result returns incorrect result rickat Excel Worksheet Functions 1 December 6th 05 01:16 PM


All times are GMT +1. The time now is 12:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"