Question

hi have to compare 2 date variables´. One of them is from a general field cell and i store it into a String variable. The other one is taken from a data field and i store it into a Date variable. The procedure i follow is: -i take the first data, the one in the normal field that is in this format "(5-8-2014)" and i work on it in the following procedure:

tempdate1 = Cells(27, "N").Value:
tempdate1 = Replace(tempdate1, "(", ""):
tempdate1 = Replace(tempdate1, ")", ""):
tempdate1 = Format(tempdate1, "DD-MM-YYYY"):

i read all the column A with a for to search the string "TIMESTAMP" and take the related date column next to it.

-Highest date is the maximun date, and at the beginning it takes value from tempdate -countdate is a variable to count the number of date i want (i want to count a date only if the date i found is greater then the local maximun) -if temp>date1 i found a local MAXIMUM, i count the TIMESTAMP and i put date in the array.

 highestDate = tempdate1
countdate = 1 ' because i have already the first date
k = 0
dates(k) = highestDate ' i put in the first position the first data
'Find the highest Date
For i = 1 To lastRow
If Cells(i, 3).Value = "TIMESTAMP" Then
temp = Cells(i, 4).Value:
temp = Format(temp, "MM-DD-YYYY"):
date1 = CDate(highestDate):
    If temp > date1 Then: date1 = temp: countdate = countdate + 1: highestDate = date1:
dates(k) = highestDate: k = k + 1
End If
Next i

Problem is that the procedure is not getting inside the if and i have no elements on the array even if in the excel sheet i have several dates greater than the first one that i found. Thank you in advance.

Was it helpful?

Solution

While VBA allows the colon, continuation character, it is generally not recommened. Example, debugging is difficult when using the colons as you can't set breakpoints.

Please set a breakpoint on

   dates(k) = highestDate

When you debug, you'll see it's how you're setting the bounds of the array:

dim dates(0)
highestDate = tempdate1
countdate = 1 ' because i have already the first date
k = 0
dates(k) = highestDate ' i put in the first position the first data
'Find the highest Date
For i = 1 To lastRow
If Cells(i, 3).Value = "TIMESTAMP" Then
temp = Cells(i, 4).Value:
temp = Format(temp, "MM-DD-YYYY"):
date1 = CDate(highestDate):
    If temp > date1 Then: date1 = temp: countdate = countdate + 1: highestDate = date1:
        dates(k) = highestDate 
        k = k + 1
        REDIM PRESERVE dates(k) 'make room in the array!
    End If
Next i
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top