Summing lists of consecutive numbers

RumbleJ

New Member
Joined
Jul 31, 2019
Messages
2
Hello, I'm trying to figure out a way to better process the data that another program that I use spits out. It gives me a list of numbers with breaks in them and I need to be able to add up all of the numbers just in the contiguous blocks of numbers like this:

AAA
1
1
2
BBB
1
1
1
4

<tbody>
</tbody>

So for this example I would need to get the answer of 4 for the first group and 7 for the second and something that would work with any arbitrary sized list. Also, sometimes it'll break apart a list (though not at a consistent length) so you'll get another list like this below the last:

BBB
3
5

<tbody>
</tbody>

So in this case then I'd be a huge help if it could see that an identical list was started previously (or that one follows) and instead return a result of 15. They will be consecutive so it's not like you'll see AAA;BBB;CCC;BBB or anything like that but there are times where it posts empty sets as well i.e.:

AAA
AAA
1
1
2

<tbody>
</tbody>

Hopefully that explains it clear enough. Thank you in advance for the help.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
The code below assumes that your data is in columns A and B. The results are output to columns G and H.

Code:
Sub GroupTotals()
Dim AR() As Variant: AR = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row).Value
Dim r As Range: Set r = Range("G1")
Dim SD As Object: Set SD = CreateObject("Scripting.Dictionary")
Dim Pos As String

For i = LBound(AR) To UBound(AR)
    If Not IsEmpty(AR(i, 1)) And IsEmpty(AR(i, 2)) Then
        Pos = AR(i, 1)
        SD.Add Pos, 0
    ElseIf Pos <> "" Then
        SD(Pos) = SD(Pos) + AR(i, 2)
    End If
Next i

Set r = r.Resize(SD.Count, 1)

With r
    .Value = Application.Transpose(SD.keys)
    .Offset(, 1).Value = Application.Transpose(SD.items)
End With

End Sub
 
Upvote 0
Thank you for your quick reply. I've been trying to figure this out using weird combinations of formulas and whatnot since I know very little about VBA. I tried to copy your code into a new module and then I edited the

Dim AR() As Variant: AR = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row).Value
Dim r As Range: Set r = Range("G1")

values to match what is in the spreadsheet but I'm not sure what to do next. Nothing happened automatically and when I try going to View Macros > Run I get Run-time error '457' This key is already associated with an element of this collection. If I knew more about VBA I might be able to tweak your code to match my use case but I'm sad to say that I am not. You'll probably need more exact parameters in this case so here's the actual format of the numbers:

junk txt
junk txt
List Name
junk txt
1
1
2

<tbody>
</tbody>

Again, I was hoping I would be able to figure at least part of this out myself but VBA really is beyond me. As you can see the List Name is actually to the right and 5 cells up from the start of the list and surrounded by some junk text (these are also columns E & F in the raw output file). This will always be the case except for when there's an empty set and the junk text and List Name repeat without any numbers in between, the right column only contains the list of numbers. Also, when I did attempt your code I inserted a copy of the right column to the left of the list since I figured that mattered. It's possible to Find & Replace the top 2 cells of junk text with blank cells but the bottom one varies with the List Name. Thank you again for the help.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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