Save to new file in range using Macro

BLuse

New Member
Joined
Apr 15, 2016
Messages
13
Hi,

I have a file with thousands of rows. I created a macro to select 500 rows, copy past in new sheet, save as .txt and delete rows.

1) How do I increase the file name by one each time. So let's say I want to save it as Book1, Book2 etc.
2) how do I repeat it over and over again until all rows are deleted.

My Macro:
ActiveWindow.ScrollRow = 1
Range("A1:A500").Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\Users\myuser\Documents\Book1", _
FileFormat:=xlText, CreateBackup:=False
ActiveWindow.SelectedSheets.Delete
ActiveWindow.ScrollRow = 1
Selection.Delete Shift:=xlUp
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, y As Long: y = 1
    LastRow = Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 1 To LastRow Step 500
        Range("A" & x).Resize(500, 1).Copy
        Sheets.Add After:=ActiveSheet
        Cells(1, 1).PasteSpecial
        ActiveSheet.Copy
        ActiveWorkbook.SaveAs Filename:="C:\Users\myuser\Documents\Book" & y, FileFormat:=xlText, CreateBackup:=False
        ActiveWorkbook.Close False
        Application.DisplayAlerts = False
        ActiveSheet.Delete
        Application.DisplayAlerts = True
        Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]").Activate
        y = y + 1
    Next x
    Application.ScreenUpdating = True
End Sub
Change the sheet name (in red) to suit your needs.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,708
Members
453,748
Latest member
akhtarf3

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