1 Attachment(s)

Data segmenting according to specific total of a column

I have a worksheet with two columns: ColA with over 320,000 rows of data of file sizes in bytes, and ColB which contains the equivelant value of ColA in megabytes.

I am in need to divide ColA data into segements separated with one empty row. Dividing should occure when the total value of ColB reaches around 900 megabyte more or less.

In addition, each segment's total of megabytes besides the total number of rows for that segment should be placed in the last row of that segment in ColC and ColD.

I am attaching part of my real data in both columns A & B in Sheet1. I have also manually caculated and segmented the first two groups of ColA in Sheet2 as an example of the final product.

Can I get some expert assistance with this request please?

Many thanks in advance.

T.

1 Attachment(s)

Re: Data segmenting according to specific total of a column

Such macro (for large files may not be quick - if you have many of them and need quicker processing - let us know) shall do the work:

Code:

`Sub test()`

Dim so_far As Double, start_row As Long, end_row As Long

start_row = 2

end_row = 2

so_far = 0

While Cells(end_row, "B") <> ""

so_far = so_far + Cells(end_row, "B").Value

If so_far > 900 Then

Cells(end_row, "C").Formula = "=sum(B" & start_row & ":B" & end_row & ")"

Cells(end_row, "D").Formula = "=count(B" & start_row & ":B" & end_row & ")"

Rows(end_row + 1).Insert

start_row = end_row + 2

end_row = end_row + 1

so_far = 0

End If

end_row = end_row + 1

Wend

If start_row <> end_row Then

end_row = end_row - 1

Cells(end_row, "C").Formula = "=sum(B" & start_row & ":B" & end_row & ")"

Cells(end_row, "D").Formula = "=count(B" & start_row & ":B" & end_row & ")"

End If

End Sub

Re: Data segmenting according to specific total of a column

More than excellent Kaper .. really fantastic .. the desired result is achieved through your genius code which ran smoothly and fast despite the large number of rows.

Thank you very much for such great work.

Please accept my deep respect and gratitude.

T.

Re: Data segmenting according to specific total of a column

Another way to do, should be faster ...!

Code:

Option Explicit

Sub Treat()

'--------------------------

' with ActiveSheet

'--------------------------

Dim MyArr

MyArr = Range(Cells(2, "B"), Cells(Rows.Count, "B").End(3))

Dim I As Integer

Dim Sum As Single

For I = LBound(MyArr, 1) To UBound(MyArr, 1)

Sum = Sum + MyArr(I, 1)

MyArr(I, 1) = ""

If Sum > 900 Then

MyArr(I, 1) = Sum

Sum = 0

End If

Next I

Range("C2").Resize(UBound(MyArr, 1)) = MyArr

Dim myAreas As Areas, myArea As Range

On Error Resume Next

Set myAreas = Columns("C").SpecialCells(xlCellTypeConstants).Areas ' CELLS CONTAINING FORMULAS

On Error GoTo 0

If myAreas Is Nothing Then Exit Sub

For Each myArea In myAreas

With myArea

.Rows(.Count).Offset(1, 0).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

End With

Next

On Error Resume Next

Set myAreas = Columns("B").SpecialCells(xlCellTypeConstants).Areas ' CELLS CONTAINING FORMULAS

On Error GoTo 0

For Each myArea In myAreas

With myArea

.Cells(.Cells.Count, 2).Formula = "=sum(" & .Address & ")"

.Cells(.Cells.Count, 3) = .Cells.Count

End With

Next

Set myAreas = Nothing

End Sub

Re: Data segmenting according to specific total of a column

Thank you very much PCI, you have always been of a great assistance to me in many issues, and here, again, you're proving that. Sure your code does the job as exactly as desired, which is enriching this question with such amazing code. I hope this question will be of great benefit to the general who have a similar issue.

Merci beaucoup, je suis très reconnaissant.

T.

Re: Data segmenting according to specific total of a column

Content d'avoir aider, à une prochaine fois.

Please to help, see for next time