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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Cheer!
BTW, could you share your latest data with null cells, and final code?
Sample Table

kindly note comment inside body of code
Try running code for Sheets with EMPTY/NULL cell Value and without

VBA Code:
Sub MergeParseII()

'----------------------------
'
'   CODE FOR MERGING VITs per SET
'
    Dim lr&, lc&, i&, j&, k&, rng, arr()
    Dim dic As Object, key
    
    lr = Cells(rows.Count, "D").End(xlUp).Row
    
'   NOTE FOR THE WOULD BE TESTER / CONTRIBUTOR
'
'   original code from the contributor
'   using below code without header / lable on row 1 generates error
'
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   new code to address header problem
'   using below code does not generates error even without lables on row 1
'
    lc = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).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
'
'   appended as advice by the contributor
'        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
    
End Sub     '   MergeParseII
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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