I have a excel file with 5 columns and more than 1000 rows. column A is customer cod. column B is year of selling machine .column C is type of machine .column D is count of selling. column E is tax. I want to create a summary in one cell (for example in column I ) to show that each cod in which years buy which types. All types in each year can get in parentheses. for example cod 1001 in 2023 buy type D, B; in 2022 buy type C; in 2021 buy type A. so my cod create :2023(D, B); 2022(C); 2021(A)
I attach my code (created via dictionaries by dear VBasic2008) and its results.
Sub TransformData()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Write to source array.
' Reference the objects.
Dim sws As Worksheet: Set sws = wb.Sheets("sheet1")
Dim srg As Range: Set srg = sws.Range("A1").CurrentRegion
' Write.
Dim sData() As Variant:
sData = srg.Value
' Write to the dictionary.
' Define.
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' Declare additional variables.
Dim r As Long, c As Long, cod As Variant, dabir As Variant, group As Variant
' Write.
For r = 2 To UBound(sData, 1) ' ÊÚÏÇÏ ˜á ÓØÑ
cod = sData(r, 1) 'cod(key)
dabir = sData(r, 2)
group = sData(r, 3)
If Not dict.Exists(cod) Then
Set dict(cod) = CreateObject("Scripting.Dictionary")
End If
If Not dict(cod).Exists(dabir) Then
Set dict(cod)(dabir) = CreateObject("Scripting.Dictionary") ' string
dict(cod)(dabir).CompareMode = vbTextCompare
End If
If Not dict(cod)(dabir).Exists(group) Then
dict(cod)(dabir)(group) = Empty
End If
Next r
' Write to the destination array.
' Define (initialize).
Dim dData() As Variant: ReDim dData(1 To dict.count + 1, 1 To 2)
r = 1
' ' Write headers.
dData(1, 1) = sData(1, 1)
dData(1, 2) = sData(1, 2) & DST_DATE_TYPE_DELIMITER & sData(1, 3)
' Declare additional variables.
Dim cKey As Variant, dKey As Variant, dStr As String
' Write data.
For Each cKey In dict.Keys
r = r + 1
dData(r, 1) = cKey
For Each dKey In dict(cKey).Keys
dStr = dStr & "; " & dKey & "(" & Join(dict(cKey)(dKey).Keys, ", ") & ")"
Next dKey
dStr = Right(dStr, Len(dStr) - Len("; "))
dData(r, 2) = dStr
dStr = vbNullString
Next cKey
' Write to the destination range.
' Reference the objects.
Dim dws As Worksheet: Set dws = wb.Sheets("Sheet1")
Dim dfcell As Range: Set dfcell = sws.Range("H1")
Dim drg As Range: Set drg = dfcell.Resize(r, 2)
' Write.
drg.Value = dData
' Clear below.
drg.Resize(dws.Rows.count - drg.Row - r + 1).Offset(r).Clear
' Format.
With drg
.Rows(1).Font.Bold = True
.EntireColumn.AutoFit
End With
End Sub
my result are here in green area

now I want to add count summation and tax summation in brackets. for example cod:1001 in 2023 buy 7 type D and -2 type B. so summation of count for cod 1001 in year 2023 is 7+(-2)=5. also the tax. summation of tax for cod 1001 in 2023 is 200,000 + 35,000=235,000. so in result I need this for cod 1001:
2023(D, B)[count=5, tax=235,000]; 2022(C)[count=2, tax=20,000]; 2021(A)[count=3, tax=150,000]
I attach the sample that in need (green area) in results.
