PC Review


Reply
Thread Tools Rate Thread

Creating mulitple workbooks from single workbook - *Macro tweaking needed*

 
 
Dan
Guest
Posts: n/a
 
      15th Feb 2007
I found this macro in the forums and it works great. What is does is
create a seperate CSV file for every row in the workbook.

What I would like to do though is create a file for every 25 rows. I
also would like to make the files .XLS, not .CSV.

Can anyone assist in tweaking this macro for me? Thanks!

Option Explicit
Sub testme()

Dim curWks As Worksheet
Dim newWks As Worksheet


Dim iRow As Long
Dim FirstRow As Long
Dim LastRow As Long


Set curWks = Worksheets("sheet1")
Set newWks = Workbooks.Add(1).Worksheets(1)


With curWks
FirstRow = 2 'headers in row 1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row


.Rows(1).Copy
With newWks.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With


For iRow = FirstRow To LastRow
.Rows(iRow).Copy
With newWks.Range("a2")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With
newWks.Parent.SaveAs _
Filename:="C:\temp\" & Format(iRow, "0000") & ".csv", _
FileFormat:=xlCSV
Next iRow
End With


newWks.Parent.Close savechanges:=False


End Sub

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      15th Feb 2007
Still keep the first row as a header row?

(Untested, but it did compile)

Option Explicit
Sub testme()

Dim curWks As Worksheet
Dim newWks As Worksheet

Dim iRow As Long
Dim FirstRow As Long
Dim LastRow As Long
Dim myStep As Long

Set curWks = Worksheets("sheet1")
Set newWks = Workbooks.Add(1).Worksheets(1)

myStep = 25

With curWks
FirstRow = 2 'headers in row 1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

.Rows(1).Copy
With newWks.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With

For iRow = FirstRow To LastRow Step myStep
.Rows(iRow).Resize(myStep).Copy
With newWks.Range("a2")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With
newWks.Parent.SaveAs _
Filename:="C:\temp\" & Format(iRow, "0000") & ".xls", _
FileFormat:=xlWorkbookNormal
Next iRow
End With

newWks.Parent.Close savechanges:=False

End Sub


Dan wrote:
>
> I found this macro in the forums and it works great. What is does is
> create a seperate CSV file for every row in the workbook.
>
> What I would like to do though is create a file for every 25 rows. I
> also would like to make the files .XLS, not .CSV.
>
> Can anyone assist in tweaking this macro for me? Thanks!
>
> Option Explicit
> Sub testme()
>
> Dim curWks As Worksheet
> Dim newWks As Worksheet
>
> Dim iRow As Long
> Dim FirstRow As Long
> Dim LastRow As Long
>
> Set curWks = Worksheets("sheet1")
> Set newWks = Workbooks.Add(1).Worksheets(1)
>
> With curWks
> FirstRow = 2 'headers in row 1
> LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
>
> .Rows(1).Copy
> With newWks.Range("A1")
> .PasteSpecial Paste:=xlPasteValues
> .PasteSpecial Paste:=xlPasteFormats
> End With
>
> For iRow = FirstRow To LastRow
> .Rows(iRow).Copy
> With newWks.Range("a2")
> .PasteSpecial Paste:=xlPasteValues
> .PasteSpecial Paste:=xlPasteFormats
> End With
> newWks.Parent.SaveAs _
> Filename:="C:\temp\" & Format(iRow, "0000") & ".csv", _
> FileFormat:=xlCSV
> Next iRow
> End With
>
> newWks.Parent.Close savechanges:=False
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      15th Feb 2007
On Feb 15, 3:31 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> Still keep the first row as a header row?
>
> (Untested, but it did compile)
>
> Option Explicit
> Sub testme()
>
> Dim curWks As Worksheet
> Dim newWks As Worksheet
>
> Dim iRow As Long
> Dim FirstRow As Long
> Dim LastRow As Long
> Dim myStep As Long
>
> Set curWks = Worksheets("sheet1")
> Set newWks = Workbooks.Add(1).Worksheets(1)
>
> myStep = 25
>
> With curWks
> FirstRow = 2 'headers in row 1
> LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
>
> .Rows(1).Copy
> With newWks.Range("A1")
> .PasteSpecial Paste:=xlPasteValues
> .PasteSpecial Paste:=xlPasteFormats
> End With
>
> For iRow = FirstRow To LastRow Step myStep
> .Rows(iRow).Resize(myStep).Copy
> With newWks.Range("a2")
> .PasteSpecial Paste:=xlPasteValues
> .PasteSpecial Paste:=xlPasteFormats
> End With
> newWks.Parent.SaveAs _
> Filename:="C:\temp\" & Format(iRow, "0000") & ".xls", _
> FileFormat:=xlWorkbookNormal
> Next iRow
> End With
>
> newWks.Parent.Close savechanges:=False
>
> End Sub
>
>
>
>
>
> Dan wrote:
>
> > I found this macro in the forums and it works great. What is does is
> > create a seperate CSV file for every row in the workbook.

>
> > What I would like to do though is create a file for every 25 rows. I
> > also would like to make the files .XLS, not .CSV.

>
> > Can anyone assist in tweaking this macro for me? Thanks!

>
> > Option Explicit
> > Sub testme()

>
> > Dim curWks As Worksheet
> > Dim newWks As Worksheet

>
> > Dim iRow As Long
> > Dim FirstRow As Long
> > Dim LastRow As Long

>
> > Set curWks = Worksheets("sheet1")
> > Set newWks = Workbooks.Add(1).Worksheets(1)

>
> > With curWks
> > FirstRow = 2 'headers in row 1
> > LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

>
> > .Rows(1).Copy
> > With newWks.Range("A1")
> > .PasteSpecial Paste:=xlPasteValues
> > .PasteSpecial Paste:=xlPasteFormats
> > End With

>
> > For iRow = FirstRow To LastRow
> > .Rows(iRow).Copy
> > With newWks.Range("a2")
> > .PasteSpecial Paste:=xlPasteValues
> > .PasteSpecial Paste:=xlPasteFormats
> > End With
> > newWks.Parent.SaveAs _
> > Filename:="C:\temp\" & Format(iRow, "0000") & ".csv", _
> > FileFormat:=xlCSV
> > Next iRow
> > End With

>
> > newWks.Parent.Close savechanges:=False

>
> > End Sub

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -


Perfect! Thanks so much!!
Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can mulitple Excel workbooks be summarized into 1 workbook? micjil Microsoft Excel Misc 1 15th Jul 2009 08:23 PM
Macro to Copy Mulitple Worksheets to New Multiple Workbooks =?Utf-8?B?SWFu?= Microsoft Excel Programming 8 13th Mar 2007 05:16 PM
RE: Copy selected mulitple worksheets to mulitple new workbooks =?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?= Microsoft Excel Programming 0 8th Mar 2007 06:57 PM
Creating a single workbook from multiple workbooks =?Utf-8?B?U2NydW0gRG93bg==?= Microsoft Excel Misc 3 6th Sep 2006 09:01 AM
Excel Copying whole Sheets from mulitple workbooks, to a target workbook Allen Microsoft Excel Programming 5 15th Mar 2004 09:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:20 AM.