Forum Discussion

Alex_Barbosa99's avatar
Alex_Barbosa99
Copper Contributor
Apr 30, 2023

How to divide cell value in many lines?

Hello,

 

I would like to create a macro to divide cell value in many lines as the following:

 

Input:

A360
B120
C240
D480
E60

 

Output:

A120
A120
A120
B120
C120
C120
D120
D120
D120
D120
E60

 

For this example I chose to divide all the cell values in multiples of 120

 

Thanks

6 Replies

  • Alex_Barbosa99 

    OliverScheurich , thanks for the idea. Tried to repeat without UI

    let
        Source = Excel.CurrentWorkbook(),
        input = Source{[Name="Input"]}[Content],
        divisor = Source{[Name="divisor"]}[Content],
    
        getRepeats = List.Zip(
            {
                input[Column1],
                List.Transform(
                    input[Column2],
                    (q) => Number.Round( Number.IntegerDivide(q, divisor[Column1]{0} ) ) )
            } ),
        repeatIt = Table.FromList(
            List.Combine(
                List.Transform( getRepeats, (q) => List.Repeat( {q{0}},
                List.Max( {1, q{1} } ) ) )
            ),
            null,
            {"Output"}
        )
    
    in
        repeatIt

    on

     

  • Alex_Barbosa99 

    Another possible solution with Power Query. In the attached file you can add data to the large blue dynamic table and a divisor in the small blue dynamic table. Then you can click in any cell of the green table and right-click with the mouse and select refresh to update the green result table.

  • Alex_Barbosa99 

    Sub expand()
    
    Dim h, i, j, k, l, m, n As Long
    
    Range("D:E").Clear
    m = 1
    h = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To h
    
    j = Cells(i, 2).Value / Range("I1").Value
    k = Application.WorksheetFunction.RoundUp(j, 0)
    
    For l = 1 To k
    Cells(m, 4).Value = Cells(i, 1).Value
    
    If Cells(i, 2).Value - n > Range("I1").Value Then
    Cells(m, 5).Value = Cells(i, 2).Value / j
    Else
    Cells(m, 5).Value = Cells(i, 2).Value - n
    End If
    n = n + Range("I1").Value
    m = m + 1
    
    Next l
    n = 0
    
    Next i
    
    End Sub

    You can try these lines of code. In the attached file you can click the button in cell H2 to run the macro. The divisor is entered in cell I2 which is 120 in this example.

  • mtarler's avatar
    mtarler
    Silver Contributor

    Alex_Barbosa99  I have been avoiding macros because of security/compatibility issues.  This could be accomplished using a formula:

    =LET(in,A2:B6,base,D2,
    baseStack,LAMBDA(inRow,base,LET(
          name, INDEX(inRow,1),
          num, INDEX(inRow,2),
          partial,MOD(num,base),
          nBases, QUOTIENT(num,base),
          outA,IF(nBases,VSTACK("",HSTACK(TEXTSPLIT(REPT(name&";",nBases),,";",1),SEQUENCE(nBases,,base,0))),""),
          DROP(IF(partial,VSTACK(outA,HSTACK(name,partial)),outA),1))),
    DROP(REDUCE("",SEQUENCE(ROWS(in)),LAMBDA(p,q,VSTACK(p,baseStack(INDEX(in,q,0),base)))),1))

    someone else may even give a more simple formula but this could also be put into a lambda in the name manager if desired.

Resources