Disaggregating data

afndst

Board Regular
Joined
Sep 8, 2015
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hello

Does anyone know VBA code to transform X into Y?

XFY
111
222
312
3

<colgroup><col width="64" span="3" style="width: 48pt; text-align: right;"> </colgroup><tbody>
</tbody>
Regards

António Teixeira
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this.

Code:
Sub Disaggregate()
Dim arrIn As Variant
Dim arrOut()
Dim I As Long
Dim J As Long
Dim cnt As Long

    arrIn = Range("A2:B4")
    
    ReDim arrOut(1 To Application.Sum(Application.Index(arrIn, , 2)), 1 To 1)
    
    For I = LBound(arrIn) To UBound(arrIn)
    
        For J = 1 To arrIn(I, 2)
            cnt = cnt + 1
            
            arrOut(cnt, 1) = arrIn(I, 1)
        Next J
    
    Next I
    
    Range("C2").Resize(UBound(arrOut)).Value = arrOut
    
End Sub
 
Upvote 0
Hello

It works perfectly.

Thank you very much

Try this.

Code:
Sub Disaggregate()
Dim arrIn As Variant
Dim arrOut()
Dim I As Long
Dim J As Long
Dim cnt As Long

    arrIn = Range("A2:B4")
    
    ReDim arrOut(1 To Application.Sum(Application.Index(arrIn, , 2)), 1 To 1)
    
    For I = LBound(arrIn) To UBound(arrIn)
    
        For J = 1 To arrIn(I, 2)
            cnt = cnt + 1
            
            arrOut(cnt, 1) = arrIn(I, 1)
        Next J
    
    Next I
    
    Range("C2").Resize(UBound(arrOut)).Value = arrOut
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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