Categories data into "baskets" VBA.

mortu90

New Member
Joined
Mar 1, 2016
Messages
8
Hi, I have a question regarding a categorization process in excel. Let me explain what I mean with that:


The information is displayed below based on rows (important to know that the "IDs" are randomly distributed all the time).

NumberID1 DescriptionID2 AmountID2 DescriptionID2 AmountID3 DescriptionID3 Amount
1Fuel1.8Discount32.12Tax15
2Discount15Tax12Fuel0.15
3Discount8Fuel44Tax12
4Tax87Discount24Fuel0.15

<tbody>
</tbody>


The above table should be "transposed" as below:


NumberFuelDiscountTax
11.832.1215
20.151512
344812
40.152487

<tbody>
</tbody>

It is quite difficult for me to even put this question into words, therefore any input is highly appreciated.

Thanks!
mortu.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:-
Results Start "J1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Sep16
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    ReDim Ray(1 To Rng.Count + 1, 1 To 4)
        Ray(1, 1) = "Number": Ray(1, 2) = "Fuel": Ray(1, 3) = "Discount": Ray(1, 4) = "Tax"
            c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    c = c + 1
    Ray(c, 1) = Dn.Value
        [COLOR="Navy"]For[/COLOR] Ac = 1 To 6 [COLOR="Navy"]Step[/COLOR] 2
            [COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] Dn.Offset(, Ac).Value
                [COLOR="Navy"]Case[/COLOR] "Fuel": Ray(c, 2) = Dn.Offset(, Ac + 1).Value
                [COLOR="Navy"]Case[/COLOR] "Discount": Ray(c, 3) = Dn.Offset(, Ac + 1).Value
                [COLOR="Navy"]Case[/COLOR] "Tax": Ray(c, 4) = Dn.Offset(, Ac + 1).Value
            [COLOR="Navy"]End[/COLOR] Select
        [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]With[/COLOR] Range("J1").Resize(c, 4)
    .Value = Ray
    .Borders.Weight = 2
    .ColumnWidth = 10
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
It works absolutely great Mick! You are an excel wizard! (y) I am trying to alter the code for my bigger project.

The only issue I have now is that the "Number" column is starting from C2 and the rest of the variables "Tax","Discount", etc are starting from CT2 (ID Description), CU2 (ID Amount), etc.


Number (C) ID1 Description (CT)ID2 Amount (CU)ID2 Description (CV)
ID2 Amount (CW)ID3 DescriptionID3 Amount
1Fuel1.8Discount32.12Tax15
2Discount15Tax12Fuel0.15
3Discount8Fuel44Tax12
4Tax87Discount24Fuel0.15


<tbody>
</tbody>


Thanks again for your input!
mortu.
 
Upvote 0
Try this for your new data layout,:-
Results in sheet2 starting "A1"
Code:
[COLOR="Navy"]Sub[/COLOR] MG27Sep16
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
    ReDim Ray(1 To Rng.Count + 1, 1 To 4)
        Ray(1, 1) = "Number": Ray(1, 2) = "Fuel": Ray(1, 3) = "Discount": Ray(1, 4) = "Tax"
            c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    c = c + 1
    Ray(c, 1) = Dn.Value
        For Ac = 95 To 101 Step 2 
            [COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] Dn.Offset(, Ac).Value
                [COLOR="Navy"]Case[/COLOR] "Fuel": Ray(c, 2) = Dn.Offset(, Ac + 1).Value
                [COLOR="Navy"]Case[/COLOR] "Discount": Ray(c, 3) = Dn.Offset(, Ac + 1).Value
                [COLOR="Navy"]Case[/COLOR] "Tax": Ray(c, 4) = Dn.Offset(, Ac + 1).Value
            [COLOR="Navy"]End[/COLOR] Select
        [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 4)
    .Value = Ray
    .Borders.Weight = 2
    .ColumnWidth = 10
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Works perfect. Thanks a lot MickG!

A last observation: I have seen that you altered the code with "For Ac = 95 To 101 Step 2" . I am trying to understand what are these numbers taken from. Initially I thought that they are based on the R1C1 reference style. I see that Ac is between 95 to 101 but CT column starts from 98. Shouldn't be For Ac = 98 To 110 Step 2 ?

For the moment I am trying with trial and error. It would be great to know this.

Regards,
mortu

 
Upvote 0
You're welcome
Ref Observation:-It based on column "C", Hence:- Set Rng = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
i.e. Ct is offset 95 columns from "C".
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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