View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default consolidate data that occurs close together in time

My apologies for not getting back to you sooner but I have been away.

I can follow your code and at this point I am not able to ascertain exactly
what the problem is without the real data. One thing you might consider is
assigning some of the date values to variables declared as double and place
stops in your code and then when the code stops, hover the cursor over the
double variables and see what they are returning. The following example of
code shows just what can occur as you manipulate dates in VBA due to rounding
of the nth decimal places and date/times that you think should be equal will
not compare as equal.

Sub testDateComparison()

Dim dateFromWs As Date
Dim dateFromStr As Date
Dim timeFromStr As Date
Dim dateCalculated As Date
Dim dblOrigDate As Double
Dim dblNewDate As Double

Dim strDate As String
Dim strTime As String

'Insert NOW() formula on worksheet
Worksheets("Sheet1").Cells(2, 1).Formula = "=Now()"

'Get date and time from the worksheet
dateFromWs = Worksheets("Sheet1").Cells(2, 1)

'Convert date and time to string format
strDate = Format(dateFromWs, "dd mm yyyy hh:mm:ss")

'Convert string format date portion back to date
dateFromStr = DateSerial(Year(strDate), Month(strDate), Day(strDate))

'Convert stringformat time portion back to date
timeFromStr = TimeSerial(Hour(strDate), Minute(strDate), Second(strDate))

'Sum the new date and time
dateCalculated = dateFromStr + timeFromStr

dblOrigDate = dateFromWs
dblNewDate = dateCalculated

'Compare the original date to the new date
'When converted to serial numbers both dates
'produce slightly different numbers.

MsgBox "Original date time as Serial = " & _
dblOrigDate & vbCrLf & _
"New date and time as Serial = " & _
dblNewDate & vbCrLf & _
"Original date time as date = " & _
dateFromWs & vbCrLf & _
"New date and time as date = " & _
dateCalculated

If dblOrigDate = dblNewDate Then
MsgBox "Dates match"
Else
MsgBox "Dates do not match"
End If

Stop
End Sub


--
Regards,

OssieMac