Organize data

Zibi

Board Regular
Joined
Feb 2, 2012
Messages
73
Hi I am trying to organize data for a report and using formulas dose not produce the results I am looking for, my VBA is very limited, please help. I need to copy first set of data and organize no next sheet. Please see example. Please help

DATA

Excel 2010
ABCD
1CatCodeItemCode2
2VEGGIE CUPS24212300000JALAPENOS SLICED1300003
3VEGGIE CUPS24209100000GARLIC PEELED1300002
4VEGGIE CUPS24204000000TOMATO CUP1300001
5STUFFED MUSHROOMS24201900000MUSHROOMS VARIETY 6 CT1200011
6STUFFED MUSHROOMS24215600000MUSHROOMS SPINACH & MOZZARELLA 2 CT1200008
7STUFFED MUSHROOMS24204200000MUSHROOMS SPINACH & MOZZARELLA 6CT1200007
8VEGGIE CUPS24210700000DICED GREEN ONION CUP1300008
9VEGGIE CUPS24214400000CELERY/ONION DICED CUP1300005
10STUFFED MUSHROOMS24201900000MUSHROOMS VARIETY 6 CT1200011
11STUFFED MUSHROOMS24215600000MUSHROOMS SPINACH & MOZZARELLA 2 CT1200008
12SOUTHWESTERN24236700000SPICY GUACAMOLE FRESH1100046
13SOUTHWESTERN24215500000PICO DE GALLO1100023
14SOUTHWESTERN24206700000BEAN DIP 7 LAYER1100014
15SOUTHWESTERN24200900000MANGO SALSA1100009
16SOUTHWESTERN24206400000JALAPENOS STUFFED W/BACON1100003
17PARTY TRAYS24210800000PREMIUM VEGETABLE TRAY1000012
18PARTY TRAYS24250100000VEGETABLE TRAY W/DIP1000007
19PARTY TRAYS24215100000FRUIT TRAY W/DIP1000005
20GRAB & GO TRAYS24218800000BROCCOLI CARROTS CAULIFLOWER & DIP800017
21GRAB & GO TRAYS24219000000BROCCOLI CARROTS GRAPE TOMATOES & DIP800015
22GRAB & GO TRAYS24218700000APPLES CHEESE & DIP800013
23GRAB & GO TRAYS24218900000BROCCOLI CARROTS & CELERY W/DIP800009
Sheet10



Desired output


Excel 2010
GHIJ
2VEGGIE CUPS
324212300000JALAPENOS SLICED1300003
424209100000GARLIC PEELED1300002
524204000000TOMATO CUP1300001
6STUFFED MUSHROOMS
724201900000MUSHROOMS VARIETY 6 CT1200011
824215600000MUSHROOMS SPINACH & MOZZARELLA 2 CT1200008
924204200000MUSHROOMS SPINACH & MOZZARELLA 6CT1200007
10VEGGIE CUPS
1124210700000DICED GREEN ONION CUP1300008
1224214400000CELERY/ONION DICED CUP1300005
13STUFFED MUSHROOMS
1424201900000MUSHROOMS VARIETY 6 CT1200011
1524215600000MUSHROOMS SPINACH & MOZZARELLA 2 CT1200008
16SOUTHWESTERN
1724236700000SPICY GUACAMOLE FRESH1100046
1824215500000PICO DE GALLO1100023
1924206700000BEAN DIP 7 LAYER1100014
2024200900000MANGO SALSA1100009
2124206400000JALAPENOS STUFFED W/BACON1100003
22PARTY TRAYS
2324210800000PREMIUM VEGETABLE TRAY1000012
2424250100000VEGETABLE TRAY W/DIP1000007
2524215100000FRUIT TRAY W/DIP1000005
26GRAB & GO TRAYS
2724218800000BROCCOLI CARROTS CAULIFLOWER & DIP800017
2824219000000BROCCOLI CARROTS GRAPE TOMATOES & DIP800015
2924218700000APPLES CHEESE & DIP800013
3024218900000BROCCOLI CARROTS & CELERY W/DIP800009
Sheet10



Thanks
 
Zibi, here is another solution using Dictionary approach which should work faster than other ones. Give it a try.

Code:
Dim dict As Object
Sub Zibi() '20/05/16
Dim i As Long, j As Long, r As Long, s As Long
Dim iar() As Variant, ar As Variant
Dim rng As Range, cell As Range, Q() As Variant

lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Set dict = CreateObject("scripting.dictionary")

Application.ScreenUpdating = False

Set rng = Sheet1.Range("A2:A" & lr)
cnt = Uniq(rng): dict.RemoveAll

iar = Sheet1.Range("A2:D" & lr)

With dict
    For i = 1 To UBound(iar, 1)
        If Not .exists(iar(i, 1)) Then
            T = 1
            r = Application.WorksheetFunction.CountIf(rng, iar(i, 1)) + 1
            ReDim Q(1 To r, 1 To 5)
            Q(T, 1) = iar(i, 1): Q(T, 5) = T + 1
            Q(T + 1, 2) = iar(i, 2): Q(T + 1, 3) = iar(i, 3): Q(T + 1, 4) = iar(i, 4)
            .Item(iar(i, 1)) = Q
        Else
            ar = .Item(iar(i, 1))
            T = ar(LBound(ar), 5) + 1
            ar(T, 2) = iar(i, 2)
            ar(T, 3) = iar(i, 3)
            ar(T, 4) = iar(i, 4)
            ar(1, 5) = T
            .Item(iar(i, 1)) = ar
        End If
    Next
    
Sheet1.Range("G1:J" & cnt + lr).ClearContents
Sheet1.Range("G1:J1") = Array("Cat", "Code", "Item", "Code2")
r = 2

For Each k In .keys
    s = .Item(k)(1, 5)
    Sheet1.Cells(r, 7).Resize(s, 5) = .Item(k)
    r = r + s
Next
Sheet1.UsedRange.Columns.AutoFit
Sheet1.Columns("K").Delete
Set dict = Nothing
End With
Application.ScreenUpdating = True
End Sub
Public Function Uniq(rng As Range) As Long
    Dim cell As Range
    For Each cell In rng.Cells
         If Not dict.exists(cell.Value) Then
            dict.Add cell.Value, 0
        End If
    Next
    Uniq = dict.Count
End Function
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I used the function Uniq for another approach but there is no need in above one. Here is the revised version after removing Function (Uniq) :

Code:
Dim dict As Object
Sub Zibi() '20/05/16
Dim i As Long, j As Long, r As Long, s As Long
Dim iar() As Variant, ar As Variant
Dim rng As Range, cell As Range, Q() As Variant

lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Set dict = CreateObject("scripting.dictionary")

Application.ScreenUpdating = False

Set rng = Sheet1.Range("A2:A" & lr)
'cnt = Uniq(rng): dict.RemoveAll

iar = Sheet1.Range("A2:D" & lr)

With dict
    For i = 1 To UBound(iar, 1)
        If Not .exists(iar(i, 1)) Then
            T = 1
            r = Application.WorksheetFunction.CountIf(rng, iar(i, 1)) + 1
            ReDim Q(1 To r, 1 To 5)
            Q(T, 1) = iar(i, 1): Q(T, 5) = T + 1
            Q(T + 1, 2) = iar(i, 2): Q(T + 1, 3) = iar(i, 3): Q(T + 1, 4) = iar(i, 4)
            .Item(iar(i, 1)) = Q
        Else
            ar = .Item(iar(i, 1))
            T = ar(LBound(ar), 5) + 1
            ar(T, 2) = iar(i, 2)
            ar(T, 3) = iar(i, 3)
            ar(T, 4) = iar(i, 4)
            ar(1, 5) = T
            .Item(iar(i, 1)) = ar
        End If
    Next
    
Sheet1.Range("G1").CurrentRegion.ClearContents
Sheet1.Range("G1:J1") = Array("Cat", "Code", "Item", "Code2")
r = 2

For Each k In .keys
    s = .Item(k)(1, 5)
    Sheet1.Cells(r, 7).Resize(s, 5) = .Item(k)
    r = r + s
Next
Sheet1.UsedRange.Columns.AutoFit
Sheet1.Columns("K").Delete
Set dict = Nothing
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi the query looks nice but for some reason it stops at:


Q(T + 1, 2) = iar(i, 2):


Can you please advise?

Thanks
 
Upvote 0
Zibi,

I hard coded it to work with 1st sheet (irrespective of its name) of your workbook on basis of index. May be you're trying to run in a different worksheet. Here is the modified version which will work with Sheet name "Sheet1."

Code:
Dim dict As Object
Sub Zibi() '20/05/16
Dim ws As Worksheet
Dim i As Long, j As Long, r As Long, s As Long
Dim iar() As Variant, ar As Variant
Dim rng As Range, cell As Range, Q() As Variant

Set ws = Worksheets("Sheet1")

lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set dict = CreateObject("scripting.dictionary")

Application.ScreenUpdating = False

Set rng = ws.Range("A2:A" & lr)

iar = ws.Range("A2:D" & lr)

With dict
    For i = 1 To UBound(iar, 1)
        If Not .exists(iar(i, 1)) Then
            T = 1
            r = Application.WorksheetFunction.CountIf(rng, iar(i, 1)) + 1
            ReDim Q(1 To r, 1 To 4)
            Q(T, 1) = iar(i, 1)
            Q(T + 1, 2) = iar(i, 2): Q(T + 1, 3) = iar(i, 3): Q(T + 1, 4) = iar(i, 4)
            .Item(iar(i, 1)) = Array(Q, T + 1)
        Else
            ar = .Item(iar(i, 1))
            T = ar(1) + 1
            ar(0)(T, 2) = iar(i, 2)
            ar(0)(T, 3) = iar(i, 3)
            ar(0)(T, 4) = iar(i, 4)
            .Item(iar(i, 1)) = Array(ar(0), T)
        End If
    Next
    
ws.Range("G1").CurrentRegion.ClearContents
ws.Range("G1:J1") = Array("Cat", "Code", "Item", "Code2")
r = 2

For Each K In .keys
    s = .Item(K)(1)
    ws.Cells(r, 7).Resize(s, 4) = .Item(K)(0)
    r = r + s
Next
ws.UsedRange.Columns.AutoFit
Set dict = Nothing
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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