Using Excel VBA, I'm trying to capture the first value in a column "Start" and the last value in a column "End", per group. Data is already sorted. Example:
I want to capture the first value for Start_open and the last value for Start_end per company. So for Company A code should put B2 in Start_Open and put C5 in Start_end.
Capturing the last value works fine using this code:
Sub First_last()
Dim i, j As Integer
Dim LastRow, LastCol As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To LastRow
If Cells(i + 1, "A").Value <> Cells(i, "A").Value Then
MsgBox i
Cells(j + 2, "E").Value = Cells(i, "C").Value
j = j + 1
End If
Next
End Sub
What I'm struggling with is capturing Start_open per group. I think I need to use above condition and use a counter to capture Start_open per group but I can't find the right code. Please advise, thanks!
You can use variables a and b to find the start and end of each section:
To add another method into the mix.
This will do what you want: