Sort and split by 2 criterias

Eddy S

New Member
Joined
Apr 22, 2013
Messages
18
Hello All
Could someone direct me to the right direction
So I have some data, for example
NameTimeCount
John14:0020
Matilda00:2011
Ben01:0030
Susan00:4510
Edd02:0014
Tom12:0040

I need to sort this in time order and then split this data into the multiple tables where in every table SUM is not more then 51 and data is not repeating, example below


NameTimeCount
Matilda
00:20​
11​
Susan
00:45​
10​
Ben
01:00​
30​
total
51​
NameTimeCount
Edd
02:00​
14​
total
14​
NameTimeCount
Tom
12:00​
40​
total
40​
NameTimeCount
John
14:00​
20​
total
20​

Also this is all done in the google sheets
Thanks in advance
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try the next version.
Your data in columns A to C, results in columns E to G

VBA Code:
Sub split_Data_2()
  Dim r As Range
  Dim i As Long, j As Long
  Dim nTotal As Double
 
  Application.ScreenUpdating = False
  Range("A:C").Copy Range("I1")
  Set r = Range("I1", Range("K" & Rows.Count).End(3))
  r.Sort Range("J1"), xlAscending, Header:=xlYes
 
  Range("E1:G1").Value = Range("A1:C1").Value
  j = 1
  For i = 2 To r.Rows.Count
    If nTotal + Range("K" & i).Value > 51 Then
      j = j + 1
      Range("F" & j).Resize(1, 2).Value = Array("total", nTotal)
      j = j + 2
      Range("E" & j).Resize(1, 3).Value = Range("A1:C1").Value
      nTotal = 0
    End If
    j = j + 1
    Range("E" & j).Resize(1, 3).Value = Range("I" & i).Resize(1, 3).Value
    nTotal = nTotal + Range("K" & i).Value
  Next
  Range("F" & j + 1).Resize(1, 2).Value = Array("total", nTotal)
  Range("I:K").ClearContents
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try the next version.
Your data in columns A to C, results in columns E to G

VBA Code:
Sub split_Data_2()
  Dim r As Range
  Dim i As Long, j As Long
  Dim nTotal As Double
 
  Application.ScreenUpdating = False
  Range("A:C").Copy Range("I1")
  Set r = Range("I1", Range("K" & Rows.Count).End(3))
  r.Sort Range("J1"), xlAscending, Header:=xlYes
 
  Range("E1:G1").Value = Range("A1:C1").Value
  j = 1
  For i = 2 To r.Rows.Count
    If nTotal + Range("K" & i).Value > 51 Then
      j = j + 1
      Range("F" & j).Resize(1, 2).Value = Array("total", nTotal)
      j = j + 2
      Range("E" & j).Resize(1, 3).Value = Range("A1:C1").Value
      nTotal = 0
    End If
    j = j + 1
    Range("E" & j).Resize(1, 3).Value = Range("I" & i).Resize(1, 3).Value
    nTotal = nTotal + Range("K" & i).Value
  Next
  Range("F" & j + 1).Resize(1, 2).Value = Array("total", nTotal)
  Range("I:K").ClearContents
  Application.ScreenUpdating = True
End Sub
Hi
This worked well for MS Excel, thanks for that . But is there any option to do this in Google sheets?
 
Upvote 0
Is here any Goole sheet master :sneaky:
There are some people here who have used Google Sheets, but whether there are any "Google Sheet Masters" here, I don't know.
Remember, this is actually an Excel forum, not a Google Sheets one! ;)
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,699
Members
452,667
Latest member
vanessavalentino83

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