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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,215,126
Messages
6,123,198
Members
449,090
Latest member
bes000

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