Close workbook at certain sheet count

aa2000

Board Regular
Joined
Aug 3, 2011
Messages
87
Hi all

I have a macro which imports text files to new sheets, but to stop the file from becoming too large, I want it to stop the import macro when the sheet count reaches 8, then open a new file and restart it.

Heres my current code

Code:
Sub Selection()
Set Rng = Tabelle2.Range("A3:A65536")
Dim b As Object
Dim SaveFile As String

SaveFile = "\Comparisons" & ".xls"
Application.SheetsInNewWorkbook = 1
Set NewBook = Workbooks.Add


For Module1.i = 3 To 15000
ThisFile = Tabelle2.Cells(i, 1).Value
    
    
        If Tabelle2.Cells(Module1.i, 1).Font.Bold = True Then
            Set NewSheet = Worksheets.Add
            With NewSheet
            ActiveSheet.name = ThisFile
            End With
            If Tabelle2.Cells(Module1.i, 1).Font.Bold = True Then Call Importselect
            End If
        If ThisWorkbook.Sheets.Count = 8 Then 
                With NewBook
                .SaveAs Filename:=ThisWorkbook.Path & SaveFile
                End With
                ActiveWorkbook.Close False
        End If

Next i

End Sub
It functions correctly, but won't stop at 8, nor will it save or close the file afterwards. How can i change this to stop at sheet 8, save it, open a new sheet and continue from where it left off?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,213,529
Messages
6,114,155
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