make it so at the end he will delete column B with those FYxx and merge those rows with same A into one row, so value from E will be lowest among them, F will be the highest among them, and columns with FY22-25 will sum
Sub ProcessData()
Dim wsData As Worksheet
Dim wsNew As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim currentRow As Long
Dim dataDict As Object
Dim key As Variant
Dim tempArray As Variant
Dim uniqueKeys As Object
Dim resultRow As Long
' Set references to worksheets
Set wsData = ThisWorkbook.Sheets("data")
' Create new worksheet
Set wsNew = ThisWorkbook.Sheets.Add
wsNew.Name = "ProcessedData_" & Format(Now(), "yyyymmdd_hhmmss")
' Get last row of data
lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
' Create dictionary to store unique A,B combinations
Set uniqueKeys = CreateObject("Scripting.Dictionary")
Set dataDict = CreateObject("Scripting.Dictionary")
' Process data and group by A,B combinations
For i = 2 To lastRow ' Assuming row 1 has headers
key = wsData.Cells(i, 1).Value & "|" & wsData.Cells(i, 2).Value
If Not dataDict.exists(key) Then
' Initialize array: MinE, MaxF, SumJ_Total, SumJ_FY22, SumJ_FY23, SumJ_FY24, SumJ_FY25, A_value, B_value, other_columns
ReDim tempArray(1 To 20) ' Adjust size as needed
tempArray(1) = wsData.Cells(i, 5).Value ' E value (will be min)
tempArray(2) = wsData.Cells(i, 6).Value ' F value (will be max)
tempArray(3) = wsData.Cells(i, 10).Value ' J value for total sum
tempArray(4) = 0 ' FY22 sum
tempArray(5) = 0 ' FY23 sum
tempArray(6) = 0 ' FY24 sum
tempArray(7) = 0 ' FY25 sum
tempArray(8) = wsData.Cells(i, 1).Value ' A value
tempArray(9) = wsData.Cells(i, 2).Value ' B value
' Store other columns (C, D, G, H, I, etc.)
tempArray(10) = wsData.Cells(i, 3).Value ' C
tempArray(11) = wsData.Cells(i, 4).Value ' D
tempArray(12) = wsData.Cells(i, 7).Value ' G
tempArray(13) = wsData.Cells(i, 8).Value ' H
tempArray(14) = wsData.Cells(i, 9).Value ' I
' Add more columns as needed
' Add FY-specific sum based on column B
Select Case wsData.Cells(i, 2).Value
Case "FY22"
tempArray(4) = wsData.Cells(i, 10).Value
Case "FY23"
tempArray(5) = wsData.Cells(i, 10).Value
Case "FY24"
tempArray(6) = wsData.Cells(i, 10).Value
Case "FY25"
tempArray(7) = wsData.Cells(i, 10).Value
End Select
dataDict.Add key, tempArray
Else
' Update existing entry
tempArray = dataDict(key)
' Update min E value
If wsData.Cells(i, 5).Value < tempArray(1) Then
tempArray(1) = wsData.Cells(i, 5).Value
End If
' Update max F value
If wsData.Cells(i, 6).Value > tempArray(2) Then
tempArray(2) = wsData.Cells(i, 6).Value
End If
' Add to total sum
tempArray(3) = tempArray(3) + wsData.Cells(i, 10).Value
' Add to FY-specific sum
Select Case wsData.Cells(i, 2).Value
Case "FY22"
tempArray(4) = tempArray(4) + wsData.Cells(i, 10).Value
Case "FY23"
tempArray(5) = tempArray(5) + wsData.Cells(i, 10).Value
Case "FY24"
tempArray(6) = tempArray(6) + wsData.Cells(i, 10).Value
Case "FY25"
tempArray(7) = tempArray(7) + wsData.Cells(i, 10).Value
End Select
dataDict(key) = tempArray
End If
Next i
' Create headers in new sheet
wsNew.Cells(1, 1).Value = "A"
wsNew.Cells(1, 2).Value = "B"
wsNew.Cells(1, 3).Value = "C"
wsNew.Cells(1, 4).Value = "D"
wsNew.Cells(1, 5).Value = "Min_E"
wsNew.Cells(1, 6).Value = "Max_F"
wsNew.Cells(1, 7).Value = "G"
wsNew.Cells(1, 8).Value = "H"
wsNew.Cells(1, 9).Value = "I"
wsNew.Cells(1, 10).Value = "Sum_J_Total"
wsNew.Cells(1, 11).Value = "Sum_J_FY22"
wsNew.Cells(1, 12).Value = "Sum_J_FY23"
wsNew.Cells(1, 13).Value = "Sum_J_FY24"
wsNew.Cells(1, 14).Value = "Sum_J_FY25"
' Format headers
With wsNew.Range("A1:N1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
.Borders.LineStyle = xlContinuous
End With
' Write processed data to new sheet
resultRow = 2
For Each key In dataDict.Keys
tempArray = dataDict(key)
wsNew.Cells(resultRow, 1).Value = tempArray(8) ' A
wsNew.Cells(resultRow, 2).Value = tempArray(9) ' B
wsNew.Cells(resultRow, 3).Value = tempArray(10) ' C
wsNew.Cells(resultRow, 4).Value = tempArray(11) ' D
wsNew.Cells(resultRow, 5).Value = tempArray(1) ' Min E
wsNew.Cells(resultRow, 6).Value = tempArray(2) ' Max F
wsNew.Cells(resultRow, 7).Value = tempArray(12) ' G
wsNew.Cells(resultRow, 8).Value = tempArray(13) ' H
wsNew.Cells(resultRow, 9).Value = tempArray(14) ' I
wsNew.Cells(resultRow, 10).Value = tempArray(3) ' Sum J Total
wsNew.Cells(resultRow, 11).Value = tempArray(4) ' Sum J FY22
wsNew.Cells(resultRow, 12).Value = tempArray(5) ' Sum J FY23
wsNew.Cells(resultRow, 13).Value = tempArray(6) ' Sum J FY24
wsNew.Cells(resultRow, 14).Value = tempArray(7) ' Sum J FY25
resultRow = resultRow + 1
Next key
' Format the data range
With wsNew.Range("A1:N" & resultRow - 1)
.Borders.LineStyle = xlContinuous
.Columns.AutoFit
End With
' Add some formatting to numeric columns
wsNew.Range("E:F,J:N").NumberFormat = "#,##0.00"
MsgBox "Data processing completed! New sheet created: " & wsNew.Name
' Clean up
Set dataDict = Nothing
Set uniqueKeys = Nothing
Set wsData = Nothing
Set wsNew = Nothing
End Sub