Split and save code

mcgeezer

New Member
Joined
Jan 20, 2014
Messages
2
I need to split a file [sheet] with potentially thousands of lines into separate workbooks, each being max 99 lines.

Thanks to searching on the site, I have developed some code for a macro that does this as per below, but it is missing one last part that I cannot get my head round. The last file generated still has 99 lines [i.e. if I Ctrl End, then it ends on a cell in row 99]. Because of how I will use the data, I need the file to end on the last row with data in it. Can you advise adaptations to cover this?

Code so far:

Sub split_and_save
Dim sh As Worksheet, newWb As Workbook, fName As String, nbr As Long
Set sh = Sheets(1)
LR = sh.Cells(Rows.Count, 1).End(xlUp).Row
fName = "C:FILE_" & Format(Now(), "yyyy-mm-dd_hh-mm-ss")
nbr = 1
For i = 1 To LR Step 99
Set newWb = Workbooks.Add
newWb.SaveAs fName & "_" & nbr & ".xls", _
FileFormat:=xlExcel5, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
sh.Range("A" & i).Resize(99, 100).Copy newWb.Sheets(1).Range("A1")
ActiveWorkbook.Close SaveChanges:=True
nbr = nbr + 1
Next
ActiveWorkbook.Close SaveChanges:=False
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Let me use an example. If the file has 150 lines, then it creates a file of 99, and then a file of 51. But the file of 51 lines isnt really 51 lines long. The code makes it fill 51 lines of a file that is 99 lines long. That then causes an issue for the system into which I am importing the data.

Similar will be true if the whole initial file is anyway less than 99 lines long.
 
Upvote 0
For the system into which I am loading these excel5.0 files, it needs to match. manually I can select the 'empty' rows down to 99 on the last file and delete them, then it imports fine.
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,184
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