Macro to export range to seperate sheet XLS format

bushidowarrior

Board Regular
Joined
Jun 27, 2011
Messages
84
Office Version
  1. 2019
Platform
  1. Windows
Okay, I am not quite sure to explain this, but here goes:


  1. When I run this marco
  2. It takes the range 2 - 6 and across filled with data.
  3. It exports the files as Title 01.xls.
  4. The name is obtained from the A1
  5. Then same happens for Title 02 and all the USED rage beneath it.
  6. The VBA look for one empty row. That is the indicator that it is one sheet.
  7. At the send I will have 2 files Title 01.xls and Title 02.xls.

I hope I have explained it well.

Thank you all very much!!!

LnAqQaE.png
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Bush,

let me know how this works.

Code:
Sub do_it()
sr = 1

For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1

If Cells(r, "A") = "" Then

lr = r - 1

myPath = "C:\Results\"
myFile = myPath & Cells(sr, "A") & ".xls"

Rows(sr & ":" & lr).Copy

  Set NewBook = Workbooks.Add
  
  NewBook.Worksheets("Sheet1").Range("A1").Select ' Special (xlPasteValues)(xlPasteformat)
    ActiveSheet.Paste

NewBook.SaveAs Filename:=myFile
ActiveWorkbook.Close

sr = r + 1
End If

Next r

End Sub

enjoy,
Ross
 
Upvote 0
Hello Almost there.

I think we have to use this:
Code:
    ActiveWorkbook.SaveAs Filename:=relativePath, FileFormat:=56 'xlExcel8 (97-2003 format xls)

and I would like the files to be create in the same folder as the excel file that is use to execute the VBA.

I am playing around with the code now.

Bush,

let me know how this works.

Code:
Sub do_it()
sr = 1

For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1

If Cells(r, "A") = "" Then

lr = r - 1

myPath = "C:\Results\"
myFile = myPath & Cells(sr, "A") & ".xls"

Rows(sr & ":" & lr).Copy

  Set NewBook = Workbooks.Add
  
  NewBook.Worksheets("Sheet1").Range("A1").Select ' Special (xlPasteValues)(xlPasteformat)
    ActiveSheet.Paste

NewBook.SaveAs Filename:=myFile
ActiveWorkbook.Close

sr = r + 1
End If

Next r

End Sub

enjoy,
Ross
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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