In my excel worksheet, Column A2:A400 contains date values for events. The row values in…
- User
In my excel worksheet, Column A2:A400 contains date values for events. The row values in column A repeat the date down column A as groups of the unique dates. Each group of rows has a unique date. Each group of rows in column A will start and end with the same unique date for the group. So when a new date starts in column A it repeats down column A as a group rows of the same date. The earliest group of dates start at the top of the column. For each row, Column E has a time value for the each activity on the Row.
The VBA code below is supposed to do the following;
Look down column A and identify each group of row of dates that match. It should then find the latest recorded time value in column E for each unique group of dates and append the text 'Close' to the same row in column V for each group of dates where the latest time is found.
Unfortunately the code below is not doing this.
Sub AppendCloseToColumnV() Dim dict As Object Set dict = CreateObject("Scripting.Dictionary")
codeDim lastRow As Long lastRow = Cells(Rows.Count, "A").End(xlUp).Row Dim i As Long Dim currentDate As Date Dim currentMaxTime As Date For i = 2 To lastRow Dim dateValue As Date Dim timeValue As Date If IsDate(Cells(i, "A").Value) Then dateValue = Cells(i, "A").Value Else GoTo NextIteration End If If IsDate(Cells(i, "E").Value) Then timeValue = Cells(i, "E").Value Else GoTo NextIteration End If If dict.Exists(dateValue) Then If dict(dateValue) < timeValue Then dict(dateValue) = timeValue End If Else dict.Add dateValue, timeValue End IfNextIteration: Next i
code' Loop through the rows to append "Close" to the rows where the latest time is found For i = 2 To lastRow If IsDate(Cells(i, "A").Value) Then currentDate = Cells(i, "A").Value currentMaxTime = dict(currentDate) If Cells(i, "E").Value = currentMaxTime Then Cells(i, "V").Value = Cells(i, "V").Value & "Close" End If End If Next i - Assistant
- User
Still not working as it should. It is bunching all the 'Close' values to the bottom rows of column V, and not in line with the same row where the latest times were found
- Assistant
Apologies for the confusion. I see the issue now. It seems that the code is not properly updating the
currentMaxTimevariable within each group. Here's an updated version of the code that should fix that issue: