Thread: Max date
View Single Post
  #18   Report Post  
 
Posts: n/a
Default

Maxi
The array
Dim myarray(1 To 12) As Integer

is equivalent to myarray(jan to dec) and stores a day value in each.
The reason you were getting incorrect results from the UDF I posted is I
had not protected against blank cells
A revised version is posted below.

The logic is as follows
Set up a data store for each possible month to hold the day value
loop through each cell in the range in turn
if there is a positive value in the cell continue if not skip to the next
cell
using the month value of the cell look in the array
if the entry in the array is not 0 then make it equal to the lowest of
either the current array value or the day value of the cell
otherwise put in the day value of the cell
when all the cells have been checked return the largest value in the array


***************************

Function MaxDate(myRange As Range) As Integer

Dim myarray(1 To 12) As Integer
For Each cell In myRange
If cell.Value 0 Then
If myarray(Month(cell.Value)) < 0 Then
myarray(Month(cell.Value)) = _
WorksheetFunction.Min(Day(cell.Value), _
myarray(Month(cell.Value)))
Else
myarray(Month(cell.Value)) = Day(cell.Value)
End If
End If
Next cell

MaxDate = WorksheetFunction.Max(myarray)

End Function

**************************

I am sure some of the gurus could simplify the logic but for now I hope
this does the trick.

let us know if it works
cheers RES