to array or not? (summing up same category in range)

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
177
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
It's been 2 weeks now since I started a mini project but my clogged brain won't cooperate with me :)
I hate to burden but can anyone help me, given the sample data I would like to merge all Med Category and its corresponding grams and value (number of Med per Unique Numbers Varies)
I hope I explained it clearly

Online File.xlsx
ABCDEFGHIJKLMNOP
1UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
2A0001PHA 12016.01.20OTCVIT - A15
3A0002PHA 22016.03.18NON-TAKERSVIT - B15VIT - E315
4A0003PHA 32016.04.19OTCVIT - C210VIT - C420VIT - C15
5A0004PHA 42016.04.22SIGNEDVIT - D315VIT - D525VIT - D15VIT - C15
6A0005PHA 52016.05.25NON-TAKERSVIT - D420VIT - E630VIT - E210
7A0006PHA 62016.06.23OTCVIT - E525
8
9
10EXPECTED OUTPUT
11UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
12A0001PHA 12016.01.20OTCVIT - A15
13A0002PHA 22016.03.18NON-TAKERSVIT - B15VIT - E315
14A0003PHA 32016.04.19OTCVIT - C735
15A0004PHA 42016.04.22SIGNEDVIT - D945VIT - C15
16A0005PHA 52016.05.25NON-TAKERSVIT - D420VIT - E840
17A0006PHA 62016.06.23OTCVIT - E525
VITs
 
Try:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr()
Dim dic As Object, key
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "D").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
rng = Range("E2", Cells(lr, lc)).Value
For i = 1 To UBound(rng)
    Set dic = CreateObject("Scripting.dictionary")
    ReDim arr(1 To 1, 1 To lc - 4)
    k = 0
    For j = 1 To UBound(rng, 2) Step 3
        If Not dic.exists(rng(i, j)) And rng(i, j) <> "" Then
            dic.Add rng(i, j), rng(i, j + 1) & "|" & rng(i, j + 2)
        Else
            dic(rng(i, 1)) = (Split(dic(rng(i, 1)), "|")(0) + rng(i, j + 1)) & "|" & (Split(dic(rng(i, 1)), "|")(1) + rng(i, j + 2))
        End If
    Next
    For Each key In dic.keys
        k = k + 1: arr(1, k) = key
        k = k + 1: arr(1, k) = Split(dic(key), "|")(0)
        k = k + 1: arr(1, k) = Split(dic(key), "|")(1)
    Next
    Range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
    Cells(i + 1, 5).Resize(1, k).Value = arr
    Set dic = Nothing
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr()
Dim dic As Object, key
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "D").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
rng = Range("E2", Cells(lr, lc)).Value
For i = 1 To UBound(rng)
    Set dic = CreateObject("Scripting.dictionary")
    ReDim arr(1 To 1, 1 To lc - 4)
    k = 0
    For j = 1 To UBound(rng, 2) Step 3
        If Not dic.exists(rng(i, j)) And rng(i, j) <> "" Then
            dic.Add rng(i, j), rng(i, j + 1) & "|" & rng(i, j + 2)
        Else
            dic(rng(i, 1)) = (Split(dic(rng(i, 1)), "|")(0) + rng(i, j + 1)) & "|" & (Split(dic(rng(i, 1)), "|")(1) + rng(i, j + 2))
        End If
    Next
    For Each key In dic.keys
        k = k + 1: arr(1, k) = key
        k = k + 1: arr(1, k) = Split(dic(key), "|")(0)
        k = k + 1: arr(1, k) = Split(dic(key), "|")(1)
    Next
    Range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
    Cells(i + 1, 5).Resize(1, k).Value = arr
    Set dic = Nothing
Next
Application.ScreenUpdating = True
End Sub
running with the following data sample
Online File.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
2A0001PHA 12016.01.20OTCVIT - A15
3A0002PHA 22016.03.18NON-TAKERSVIT - B15VIT - E315
4A0003PHA 32016.04.19OTCVIT - C210VIT - C420VIT - C15VIT - C15VIT - C15
5A0004PHA 42016.04.22SIGNEDVIT - D315VIT - D525VIT - D15VIT - C15VIT - D15VIT - C15
6A0005PHA 52016.05.25NON-TAKERSVIT - D420VIT - E630VIT - E210VIT - C15VIT - E210
VITs (19)

Row 5 & 6 computations seems a bit off, would you care mate running with the above data ....
 
Upvote 0
This is how it looks after code running
Book1
ABCDEFGHIJKLMNOPQRSTUV
1UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
2A0001PHA 120/01/2016OTCVIT - A15
3A0002PHA 218/03/2016NON-TAKERSVIT - B15VIT - E315
4A0003PHA 319/04/2016OTCVIT - C945
5A0004PHA 422/04/2016SIGNEDVIT - D1155VIT - C15
6A0005PHA 525/05/2016NON-TAKERSVIT - D840VIT - E630VIT - C15
Sheet1
 
Upvote 0
This is how it looks after code running
Book1
ABCDEFGHIJKLMNOPQRSTUV
1UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
2A0001PHA 120/01/2016OTCVIT - A15
3A0002PHA 218/03/2016NON-TAKERSVIT - B15VIT - E315
4A0003PHA 319/04/2016OTCVIT - C945
5A0004PHA 422/04/2016SIGNEDVIT - D1155VIT - C15
6A0005PHA 525/05/2016NON-TAKERSVIT - D840VIT - E630VIT - C15
Sheet1
Output after Processed (EXPECTED RESULT)

Online File.xlsx
ABCDEFGHIJKLMNOPQRSTUV
17UNIQUE NUMBERUNDERDATECLASSMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValueMEDGramsValue
18A0001PHA 142389OTCVIT - A15
19A0002PHA 242447NON-TAKERSVIT - B15VIT - E315
20A0003PHA 342479OTCVIT - C945
21A0004PHA 442482SIGNEDVIT - D1050VIT - C210
22A0005PHA 542515NON-TAKERSVIT - D420VIT - E1050VIT - C15
Sheet18
 
Upvote 0
Try again:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr()
Dim dic As Object, key
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "D").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
rng = Range("E2", Cells(lr, lc)).Value
For i = 1 To UBound(rng)
    Set dic = CreateObject("Scripting.dictionary")
    ReDim arr(1 To 1, 1 To lc - 4)
    k = 0
    For j = 1 To UBound(rng, 2) Step 3
    If rng(i, j) <> "" Then
        If Not dic.exists(rng(i, j)) Then
            dic.Add rng(i, j), rng(i, j + 1) & "|" & rng(i, j + 2)
        Else
            dic(rng(i, j)) = Split(dic(rng(i, j)), "|")(0) + rng(i, j + 1) & "|" & Split(dic(rng(i, j)), "|")(1) + rng(i, j + 2)
        End If
    End If
    Next
    For Each key In dic.keys
        k = k + 1: arr(1, k) = key
        k = k + 1: arr(1, k) = Split(dic(key), "|")(0)
        k = k + 1: arr(1, k) = Split(dic(key), "|")(1)
    Next
    Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
    Range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
    Cells(i + 1, 5).Resize(1, k).Value = arr
    dic.RemoveAll
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try again:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr()
Dim dic As Object, key
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "D").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
rng = Range("E2", Cells(lr, lc)).Value
For i = 1 To UBound(rng)
    Set dic = CreateObject("Scripting.dictionary")
    ReDim arr(1 To 1, 1 To lc - 4)
    k = 0
    For j = 1 To UBound(rng, 2) Step 3
    If rng(i, j) <> "" Then
        If Not dic.exists(rng(i, j)) Then
            dic.Add rng(i, j), rng(i, j + 1) & "|" & rng(i, j + 2)
        Else
            dic(rng(i, j)) = Split(dic(rng(i, j)), "|")(0) + rng(i, j + 1) & "|" & Split(dic(rng(i, j)), "|")(1) + rng(i, j + 2)
        End If
    End If
    Next
    For Each key In dic.keys
        k = k + 1: arr(1, k) = key
        k = k + 1: arr(1, k) = Split(dic(key), "|")(0)
        k = k + 1: arr(1, k) = Split(dic(key), "|")(1)
    Next
    Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
    Range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
    Cells(i + 1, 5).Resize(1, k).Value = arr
    dic.RemoveAll
Next
Application.ScreenUpdating = True
End Sub
my question is can you explain the following code: (what is the constant value for and where did you get it?)
because appending your code to my Main code generates RT 1004 error but using it as stand-alone works a beauty and does not generates such error

VBA Code:
ReDim arr(1 To 1, 1 To lc - 4)

Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
Cells(i + 1, 5).Resize(1, k).Value = arr
 
Upvote 0
my question is can you explain the following code: (what is the constant value for and where did you get it?)
because appending your code to my Main code generates RT 1004 error but using it as stand-alone works a beauty and does not generates such error

VBA Code:
ReDim arr(1 To 1, 1 To lc - 4)

Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents
Cells(i + 1, 5).Resize(1, k).Value = arr
Sorry, I forgot to remove this line. Its just for testing during coding.
VBA Code:
Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
Then, should be:
VBA Code:
ReDim arr(1 To 1, 1 To lc - 4) ' the output array. lc = Last column index. lc-4 = columns count from column E to the right
range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents ' clear the old values from column E to the right
Cells(i + 1, 5).Resize(1, k).Value = arr' paste value from arr to column E to the right
 
Upvote 0
Sorry, I forgot to remove this line. Its just for testing during coding.
VBA Code:
Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
Then, should be:
VBA Code:
ReDim arr(1 To 1, 1 To lc - 4) ' the output array. lc = Last column index. lc-4 = columns count from column E to the right
range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents ' clear the old values from column E to the right
Cells(i + 1, 5).Resize(1, k).Value = arr' paste value from arr to column E to the right
OTWH, will clear my head and tackle my RT error tomorrow! i hope you could still lend a hand thanks mate....
 
Upvote 0
Sorry, I forgot to remove this line. Its just for testing during coding.
VBA Code:
Cells(i + 1, 23).Resize(1, dic.Count).Value = dic.items
Then, should be:
VBA Code:
ReDim arr(1 To 1, 1 To lc - 4) ' the output array. lc = Last column index. lc-4 = columns count from column E to the right
range(Cells(i + 1, 5), Cells(i + 1, 10000)).ClearContents ' clear the old values from column E to the right
Cells(i + 1, 5).Resize(1, k).Value = arr' paste value from arr to column E to the right
finally!
after a couple of days staring at my code the target data, I managed to pinpoint the culprit!
the code works smoothly but when it encounter a null/empty value in its range it somehow breaks the code and generates error!
what I did was before running the code I deleted (via VBA) all null/empty cell since it wont be needed anyway!
lo and behold the power of community helping each other thanks mate really appreciate it!
I always try to solved my code problem before asking for help and always every time people really helped! thanks again...
 
Upvote 0
Cheer!
BTW, could you share your latest data with null cells, and final code?
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top