Values in column A are all date values in the format dddd dd/mm/yy. Values in column E…
- User
Values in column A are all date values in the format dddd dd/mm/yy. Values in column E are all time values in the format 00:00:00 PM
For each row there is a time value in column E. The time values in column E are sometimes repeated.
In column A the date values are repeated in groups of the exact date. The number of rows containing the same dates that for a group vary.
I want a VBA code that will search column A, Identify the rows that contain a group of exact dates, and find the time value in column E of the group that is the latest time. Then in the row where the latest time is found for that group, append the text value 'Close' to column V.
Then continue downwards column A to identify the next group of rows that contain the exact date and repepaet the process of determining the latest time value found in column E for that group and append the text value 'Close' to the relevant row in column V
I have tried using this code but keep getting a type mismatch error.
Sub AddCloseTextToLatestTime() Dim lastRow As Long, i As Long Dim currentGroup As String, currentGroupEndTime As Date
'Get the last row in column A lastRow = Cells(Rows.Count, "A").End(xlUp).row
'Loop through each row in column A For i = 2 To lastRow 'Check if it’s a new group If CStr(Cells(i, "A").Value) <> currentGroup Then 'Update the current group and end time currentGroup = CStr(Cells(i, "A").Value) currentGroupEndTime = timeValue(CDate(Cells(i, "E").Value)) Else 'Check if the current time is greater than the previous end time If timeValue(CDate(Cells(i, "E").Value)) > currentGroupEndTime Then 'Update the end time for the current group currentGroupEndTime = timeValue(CDate(Cells(i, "E").Value)) End If End If
'Check if it’s the last row in the group or the entire column If i = lastRow Or CStr(Cells(i + 1, "A").Value) <> currentGroup Then 'Find the row with the latest time value in the group For j = i To 2 Step -1 If CStr(Cells(j, "A").Value) = currentGroup Then 'Check if the current row has the latest end time If timeValue(CDate(Cells(j, "E").Value)) = currentGroupEndTime Then 'Add "Close" to column V of the current row Cells(j, "V").Value = "Close" Else 'Exit the loop if it’s not the latest time Exit For End If Else 'Exit the loop if the group has changed Exit For End If Next j End IfNext i End Sub
- Assistant
The type mismatch error is likely occurring because of the following line of code:
currentGroupEndTime = timeValue(CDate(Cells(i, "E").Value))
The function "timeValue()" is not a recognized function in VBA. To correct this, you can use the TimeValue() function instead. Additionally, you can simplify the code by using a Dictionary object to store the latest time value for each group. Here is an updated version of the code: