Expand Data set based on a Summary Table

hassanleo1987

Board Regular
Joined
Apr 19, 2017
Messages
56
Hi!

I would like to request the help of VBA experts on a tedious & time consuming problem I have facing.
Currently I have a daily data summary in a compressed / summarized format that I need to expand vertically which later will be incorporated in a database of future analysis and forecasting.
The current data structure is as following:

Sample.xlsx
ABCDEFGH
3Data_NMax.ValAAABBBCCCDDDEEEFFF
4A6516301
5B8625852
6C6234261
7D5343105
8E6052436
9F8161558
Sheet1
Cell Formulas
RangeFormula
B4:B9B4=MAX(C4:H4)


The Column B has the max value of Item A for next 6 instances i.e., max value from Column AAA to FFF is 6

The desired Data structure should expand vertically where A is repeated in consective rows until max value of rows are filled. (6 rows with A).
Next Column AAA to FFF are filled with a repeated value, lets say 1 as per each column's respective number is current data set. AAA=5, BBB=1 and so on for Data_N = A.

Same senario is implemented on each distinct value in Data_N. Resultant Data Struture should be as following:

Required Expanded Data Model
Data_NAAABBBCCCDDDEEEFFF
A11111
A111
A111
A11
A11
A1
B111111
B111111
B1111
B1111
B1111
B11
B1
B1
C111111
C11111
C111
C11
C1
C1
D11111
D1111
D1111
D11
D1
E11111
E11111
E1111
E111
E11
E1
F111111
F1111
F1111
F1111
F1111
F11
F1
F1


The solution can be single or multiple sheet.

Kindly look into this problem and advice of a solution with VBA.

Thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
@hassanleo1987 Does this help?

VBA Code:
Sub Re_Structure()

Dim dnr As Long
Dim mv As Integer
Dim lr As Long
Dim r As Long
Dim lnr As Long
Dim ldr As Long
Dim MaxVal As Long
Dim DataN As String
Dim Rng As Range

'******* Assumes headers existK3:Q3  ??

Application.ScreenUpdating = False   'Aid speed and visuals by suspending screen updating

'***** Clear any existing K4:Q data  ??????
lr = Cells(Rows.Count, 11).End(xlUp).Row + 1  'last row in K

Range("K4:Q" & lr).ClearContents

'Get last row in column A
lr = Cells(Rows.Count, 1).End(xlUp).Row
'dnr = start row of column K for current  Data_N
dnr = 4  'initially row 4


'loop through all rows of column A
For r = 4 To lr
    MaxVal = Cells(r, 2)   'Max.Val
    DataN = Range("A" & r)  'Data_N value
    
    ldr = dnr + MaxVal - 1   'ldr  last data  row in K for current Data_N
    
    Set Rng = Range("K" & dnr & ":K" & ldr)    'K range for current Data_N

    Rng.Value = DataN    ' Apply Data_N to range in K
    
    dnr = ldr + 1  'update start row for next Data_N in K
      
      'Loop through offset columns to get each number of instances, n
        For c = 1 To 6
        
            n = Cells(r, 2).Offset(0, c)  'number of instances found in offset from column 2. ie B
            'insert 1 n times in column K offset
            If n > 0 Then Rng.Offset(0, c).Resize(n, 1).Value = 1  'Ignore and prevent an errorif n = 0
            
        Next c  'Next offset


Next r  'next row of data in A

Application.ScreenUpdating = True   'Re-establish screen updating.

End Sub



Book1
ABCDEFGHIJKLMNOPQ
1
2
3Data_NMax.ValAAABBBCCCDDDEEEFFFData_NAAABBBCCCDDDEEEFFF
4A6516301A11111
5B8625852A111
6C6234261A111
7D5343105A11
8E6052436A11
9F8161558A1
10B111111
11B111111
12B1111
13B1111
14B1111
15B11
16B1
17B1
18C111111
19C11111
20C111
Sheet1
Cell Formulas
RangeFormula
B4:B9B4=MAX(C4:H4)
 
Upvote 0
Solution
Hi, see the linked file for a possible solution (without VBA)...

The formulas used in the table...
Sheet1!I4: =IF(A4="","",SUM(B$4:B4)) (Range: I4:I10...)
Sheet1!A4: =IF(ROW()>SUM(Sheet1!B:B)+3,"",INDEX(Sheet1!A:A,IFERROR(MATCH(ROW()-4,Sheet1!I:I),3)+1)) (Range: A4:A50...)
Sheet1!B4: =IF(ROW()>SUM(Sheet1!$B:$B)+3,"",IF(INDEX(Sheet1!C:C,IFERROR(MATCH(ROW()-4,Sheet1!$I:$I),3)+1)>ROW()-4-IFERROR(INDEX(Sheet1!$I:$I,MATCH(ROW()-4,Sheet1!$I:$I)),0),1,"")) (Range: B4:G50...)

Expand.xlsx

Sheet1.png


Sheet2a.png


Sheet2b.png
 
Upvote 0
@hassanleo1987 Does this help?

VBA Code:
Sub Re_Structure()

Dim dnr As Long
Dim mv As Integer
Dim lr As Long
Dim r As Long
Dim lnr As Long
Dim ldr As Long
Dim MaxVal As Long
Dim DataN As String
Dim Rng As Range

'******* Assumes headers existK3:Q3  ??

Application.ScreenUpdating = False   'Aid speed and visuals by suspending screen updating

'***** Clear any existing K4:Q data  ??????
lr = Cells(Rows.Count, 11).End(xlUp).Row + 1  'last row in K

Range("K4:Q" & lr).ClearContents

'Get last row in column A
lr = Cells(Rows.Count, 1).End(xlUp).Row
'dnr = start row of column K for current  Data_N
dnr = 4  'initially row 4


'loop through all rows of column A
For r = 4 To lr
    MaxVal = Cells(r, 2)   'Max.Val
    DataN = Range("A" & r)  'Data_N value
   
    ldr = dnr + MaxVal - 1   'ldr  last data  row in K for current Data_N
   
    Set Rng = Range("K" & dnr & ":K" & ldr)    'K range for current Data_N

    Rng.Value = DataN    ' Apply Data_N to range in K
   
    dnr = ldr + 1  'update start row for next Data_N in K
     
      'Loop through offset columns to get each number of instances, n
        For c = 1 To 6
       
            n = Cells(r, 2).Offset(0, c)  'number of instances found in offset from column 2. ie B
            'insert 1 n times in column K offset
            If n > 0 Then Rng.Offset(0, c).Resize(n, 1).Value = 1  'Ignore and prevent an errorif n = 0
           
        Next c  'Next offset


Next r  'next row of data in A

Application.ScreenUpdating = True   'Re-establish screen updating.

End Sub



Book1
ABCDEFGHIJKLMNOPQ
1
2
3Data_NMax.ValAAABBBCCCDDDEEEFFFData_NAAABBBCCCDDDEEEFFF
4A6516301A11111
5B8625852A111
6C6234261A111
7D5343105A11
8E6052436A11
9F8161558A1
10B111111
11B111111
12B1111
13B1111
14B1111
15B11
16B1
17B1
18C111111
19C11111
20C111
Sheet1
Cell Formulas
RangeFormula
B4:B9B4=MAX(C4:H4)
@Snakehips Thanks a lot. It works perfectly and will save me a great deal of time.
 
Upvote 0
Hi, see the linked file for a possible solution (without VBA)...

The formulas used in the table...
Sheet1!I4: =IF(A4="","",SUM(B$4:B4)) (Range: I4:I10...)
Sheet1!A4: =IF(ROW()>SUM(Sheet1!B:B)+3,"",INDEX(Sheet1!A:A,IFERROR(MATCH(ROW()-4,Sheet1!I:I),3)+1)) (Range: A4:A50...)
Sheet1!B4: =IF(ROW()>SUM(Sheet1!$B:$B)+3,"",IF(INDEX(Sheet1!C:C,IFERROR(MATCH(ROW()-4,Sheet1!$I:$I),3)+1)>ROW()-4-IFERROR(INDEX(Sheet1!$I:$I,MATCH(ROW()-4,Sheet1!$I:$I)),0),1,"")) (Range: B4:G50...)

Expand.xlsx

View attachment 89833

View attachment 89834

View attachment 89835
@fjns , Your solution is also perfect but for a smaller data as I had presented in the sample, But I have a large data set that needs a VBA solution to minimize the processing time.
Thanks a lot for you effort!
 
Upvote 0
Hi, I am very glad that the formulas work.
If I have enough knowledge, I am happy to help at any time.
 
Upvote 0

Forum statistics

Threads
1,216,743
Messages
6,132,463
Members
449,729
Latest member
davelevnt

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