Consulting

Results 1 to 7 of 7

Thread: A Simple Pyramid Totals

  1. #1
    VBAX Newbie
    Joined
    Mar 2016
    Posts
    3
    Location

    A Simple Pyramid Totals

    Hi everyone.

    Is there a easy way to sum different rows in a worksheet to create a subtotal? I just want to use sum formula (sum(A1,A5,A9) etc.) not sumifs or sumproduct. I know it should be really easy but I couldnt do it.


    Best.
    Attached Files Attached Files

  2. #2
    VBAX Newbie
    Joined
    Mar 2016
    Posts
    3
    Location
    Any ideas?

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to vbax etmzkn

    your workbook tells me nothing about your requirement.

    so i take into account the first message and assume you want to sum non-contiguous cells (i assume you mean cells by rows)

    if this is what you are after, it's too simple, try:

    1st method
    select the cell yo want the sum values.
    press AutoSum. Excel will insert a SUM() function for the closest possible range.
    clear the range in formula
    select the first cell to sum, hold down the Ctrl key and select second cell.
    select as many cells as you wish to add while holding down Ctrl key.
    press Enter

    you will see a formula like
    PHP Code:
    =SUM(F13,F20,H22,H30
    2nd method
    select the cell
    press = key
    then select the first cell, press + key, select second cell
    select as many cells as you wish to add by inputting + between cells
    press Enter
    you will see a formula like
    PHP Code:
    =F13+F20+H22+H30 

    if this is not what you are after, try to explain clearly what you need...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Newbie
    Joined
    Mar 2016
    Posts
    3
    Location
    First of all thank you for your reply.

    I have updated sample worksheet with descriptions. Also you can see my try on VBA view.

    Hope I could explain myself.

    Best.
    Attached Files Attached Files

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    yes, thats it. now you now how to demonstrate your questions / requirements here.

    i'm quite busy at the moment and will work on it later.

    other members may post a solution too.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i assume your table is sorted ascending based on column B.

    the order is
    1
    11
    111
    2
    22
    222
    2222

    and not
    1
    12
    2121
    11
    211
    2
    1111

    and i assume maximum heading level is 4
    1
    11
    111
    1111

    not
    1
    11
    111
    1111
    11111
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_55537_multi_level_subtotals()
    
        Dim i As Long, j As Long, LevelSum As Double
    
        'Summing Level4 numbers for Level3 values
        For i = Cells(Rows.Count, 2).End(xlUp).Row To 3 Step -1
            If Len(Cells(i, 2)) = 4 Then
                LevelSum = 0
                LevelSum = LevelSum + Cells(i, 3)
                For j = i - 1 To 3 Step -1
                    If Len(Cells(j, 2)) = 3 Then
                        Cells(j, 3) = LevelSum
                        i = j
                        Exit For
                    ElseIf Len(Cells(j, 2)) = 4 Then
                        LevelSum = LevelSum + Cells(j, 3)
                    End If
                Next j
            End If
        Next i
        
        'Summing Level3 numbers for Level2 values
        For i = Cells(Rows.Count, 2).End(xlUp).Row To 3 Step -1
            If Len(Cells(i, 2)) = 3 Then
                LevelSum = 0
                LevelSum = LevelSum + Cells(i, 3)
                For j = i - 1 To 3 Step -1
                    If Len(Cells(j, 2)) = 2 Then
                        Cells(j, 3) = LevelSum
                        i = j
                        Exit For
                    ElseIf Len(Cells(j, 2)) = 3 Then
                        LevelSum = LevelSum + Cells(j, 3)
                    End If
                Next j
            End If
        Next i
        
        'Summing Level2 numbers for Level1 values
        For i = Cells(Rows.Count, 2).End(xlUp).Row To 3 Step -1
            If Len(Cells(i, 2)) = 2 Then
                LevelSum = 0
                LevelSum = LevelSum + Cells(i, 3)
                For j = i - 1 To 3 Step -1
                    If Len(Cells(j, 2)) = 1 Then
                        Cells(j, 3) = LevelSum
                        i = j
                        Exit For
                    ElseIf Len(Cells(j, 2)) = 2 Then
                        LevelSum = LevelSum + Cells(j, 3)
                    End If
                Next j
            End If
        Next i
    
    End Sub
    PS: before running the code, make sure the cells into which the sums will be inserted are cleared.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •