Link to home
Start Free TrialLog in
Avatar of Jimi Sherman
Jimi ShermanFlag for United States of America

asked on

How to Split a list?

I need a Macro to split a long list into parts at every 350 rows with each part on a separate sheet.
I have attached two files so you can see an example.
Thanks,
Jimi
Routes-2013-9-24.xlsx
Routes-2013-9-24-Final.xlsx
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Try this macro in the attached workbook.

Sub SplitData()
Dim lngLastRow As Long
Dim lngIndex As Long

lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 2 To lngLastRow Step 350
    Sheets("Sheet1").Range("A1:F1", "A" & lngIndex & ":F" & lngIndex + 349).Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Paste
Next
End Sub

Open in new window


BTW your original file would be much smaller if you deleted all or most of the blank rows in Sheet1.
Q-28248378.xlsm
Avatar of Jimi Sherman

ASKER

Hi,
When I run this only sheet2 has 350 rows the other sheets has over 700. I was looking for it to be like the "after" file I attached only 350 rows per sheet or less.
Thanks
Building on MartinLiss' code, try something like:

Option Explicit

Sub SplitData()
Dim lngLastRow As Long
Dim lngIndex As Long

lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 2 To lngLastRow Step 350
    Sheets("Sheet1").Range("A1:F1", "A" & lngIndex & ":F" & lngIndex + 349).Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Paste
    Debug.Print lngIndex & "  " & lngIndex + 349
    Sheets("Sheet1").Range("A2:F351").Delete
Next
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works perfect!
Thanks
You're welcome and I'm glad I was able to help.  And I'm sorry I didn't notice that the first time.

Marty - MVP 2009 to 2013