"next" function not working

xxgirlinbluexx

New Member
Joined
Jun 17, 2013
Messages
5
I wrote a macro to split a file into rows of 20 and save them each in separate workbooks. It worked the first time round. However, I'm trying the macro a few days after and it no longer works! I only get 1 file with the first 20 rows and the macro stops there. There appear to be no bugs at all. Any way I can fix the problem? I have copied the code here for reference:

Code:
Sub SplitSheet()
'Split data lines into rows of n lines each and save into another workbook
 
Dim lastRow As Long, myRow As Long, myBook As Workbook
lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For myRow = 1 To lastRow Step 20
    Set myBook = Workbooks.Add
    ThisWorkbook.Sheets("Sheet1").Rows(myRow & ":" & myRow + 19).EntireRow.Copy myBook.Sheets("Sheet1").Range("A1")
With ActiveWorkbook
                .SaveAs Filename:="C:/File" & myRow & ".xls"
                .Close
            End With
 
 
Next myRow
End Sub

Any help is appreciated!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Just to add on, the original document that I am splitting up has 17000 rows and 7 columns of data

I wrote a macro to split a file into rows of 20 and save them each in separate workbooks. It worked the first time round. However, I'm trying the macro a few days after and it no longer works! I only get 1 file with the first 20 rows and the macro stops there. There appear to be no bugs at all. Any way I can fix the problem? I have copied the code here for reference:

Code:
Sub SplitSheet()
'Split data lines into rows of n lines each and save into another workbook
 
Dim lastRow As Long, myRow As Long, myBook As Workbook
lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For myRow = 1 To lastRow Step 20
    Set myBook = Workbooks.Add
    ThisWorkbook.Sheets("Sheet1").Rows(myRow & ":" & myRow + 19).EntireRow.Copy myBook.Sheets("Sheet1").Range("A1")
With ActiveWorkbook
                .SaveAs Filename:="C:/File" & myRow & ".xls"
                .Close
            End With
 
 
Next myRow
End Sub

Any help is appreciated!
 
Upvote 0
Does this work?
Code:
Sub SplitSheet()
Dim lastRow As Long, myRow As Long, myBook As Workbook
    
    'Split data lines into rows of n lines each and save into another workbook

    lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    
    For myRow = 1 To lastRow Step 20
    
        Set myBook = Workbooks.Add
        
        ThisWorkbook.Sheets("Sheet1").Rows(myRow & ":" & myRow + 19).EntireRow.Copy myBook.Sheets("Sheet1").Range("A1")
        
        With myBook
            .SaveAs Filename:="C:/File" & myRow & ".xls"
            .Close
        End With
        
    Next myRow
    
End Sub
 
Upvote 0
What is the value of LastRow at the time the loop starts ?
You can open the Locals Window in the VBA window, and step through the code with VBA.

My guess would be that the LastRow is <20

It could be the use of ThisWorkbook, that refers to the Book that contains the code.
Maybe you could try ActiveWorkbook instead.
Or better yet, fully specify the correct book, Workbooks("bookname.xlsx")
 
Upvote 0
How about this? I named the original workbook "Workbook2.xlsm" where the last row of data is at 17387. However, now the code doesnt even generate a file. Am I missing something?
Code:
Sub SplitSheet()
'Split data lines into rows of n lines each and save into another workbook


Dim lastRow As Long, myRow As Long, myBook As Workbook
For myRow = 1 To lastRow = 17387 Step 20
    Set myBook = Workbooks.Add
    Workbooks("Workbook2.xlsm").Sheets("Sheet1").Rows(myRow & ":" & myRow + 19).EntireRow.Copy myBook.Sheets("Sheet1").Range("A1")
With ActiveWorkbook
                .SaveAs Filename:="C:/File" & myRow & ".xls"
                .Close
            End With




Next myRow
End Sub



What is the value of LastRow at the time the loop starts ?
You can open the Locals Window in the VBA window, and step through the code with VBA.

My guess would be that the LastRow is <20

It could be the use of ThisWorkbook, that refers to the Book that contains the code.
Maybe you could try ActiveWorkbook instead.
Or better yet, fully specify the correct book, Workbooks("bookname.xlsx")
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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