Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Split workbook into multiple workbooks

I have a workbook with 95 worksheets in it, and I'd like to be able to split
these out into individual workbooks. I looked on Ron DeBruin's site and got
what you see below, although I'm having trouble with it. I am certain that
I'm missing the forest for the trees, but when I run this coding it saves the
"Personal" workbook rather than the workbook that I want to save. I did a
test by adding another worksheet to the personal workbook, and sure enough it
split both of them out. How can I get it to choose the right workbook?
Changing "ThisWorkbook" to the workbook name didn't do it, and neither did
"Windows("WorkbookBreakupTest.xls").Activate" at the beginning of the coding
(WorkbookBreakupTest is the name of the file). I really, really appreciate
your help.

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String

With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With

'Copy every sheet from the workbook with this macro
Set Sourcewb = ThisWorkbook

'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName

'Copy every visible sheet to a new workbook
For Each sh In Sourcewb.Worksheets

'If the sheet is visible then copy it to a new workbook
If sh.Visible = -1 Then
sh.Copy

'Set Destwb to the new workbook
Set Destwb = ActiveWorkbook

'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
If Sourcewb.Name = .Name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With

'Change all cells in the worksheet to values if you want
If Destwb.Sheets(1).ProtectContents = False Then
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
End If


'Save the new workbook and close it
With Destwb
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & FileExtStr, _
FileFormat:=FileFormatNum
.Close False
End With

End If
GoToNextSheet:
Next sh

MsgBox "You can find the files in " & FolderName

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub

--
Message posted via http://www.officekb.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Split workbook into multiple workbooks

This line:

Set Sourcewb = ThisWorkbook

says to use the workbook with the code.

If that's not what you want, you can keep the code in your personal.xls
workbook, but change the line to:

Set Sourcewb = ActiveWorkbook

Just make sure the workbook that should be split is active before you run the
macro.

"Joe_Hunt via OfficeKB.com" wrote:

I have a workbook with 95 worksheets in it, and I'd like to be able to split
these out into individual workbooks. I looked on Ron DeBruin's site and got
what you see below, although I'm having trouble with it. I am certain that
I'm missing the forest for the trees, but when I run this coding it saves the
"Personal" workbook rather than the workbook that I want to save. I did a
test by adding another worksheet to the personal workbook, and sure enough it
split both of them out. How can I get it to choose the right workbook?
Changing "ThisWorkbook" to the workbook name didn't do it, and neither did
"Windows("WorkbookBreakupTest.xls").Activate" at the beginning of the coding
(WorkbookBreakupTest is the name of the file). I really, really appreciate
your help.

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String

With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With

'Copy every sheet from the workbook with this macro
Set Sourcewb = ThisWorkbook

'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName

'Copy every visible sheet to a new workbook
For Each sh In Sourcewb.Worksheets

'If the sheet is visible then copy it to a new workbook
If sh.Visible = -1 Then
sh.Copy

'Set Destwb to the new workbook
Set Destwb = ActiveWorkbook

'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
If Sourcewb.Name = .Name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With

'Change all cells in the worksheet to values if you want
If Destwb.Sheets(1).ProtectContents = False Then
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
End If

'Save the new workbook and close it
With Destwb
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & FileExtStr, _
FileFormat:=FileFormatNum
.Close False
End With

End If
GoToNextSheet:
Next sh

MsgBox "You can find the files in " & FolderName

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub

--
Message posted via http://www.officekb.com


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Split workbook into multiple workbooks

That was perfect! Thank you very much!

Dave Peterson wrote:
This line:

Set Sourcewb = ThisWorkbook

says to use the workbook with the code.

If that's not what you want, you can keep the code in your personal.xls
workbook, but change the line to:

Set Sourcewb = ActiveWorkbook

Just make sure the workbook that should be split is active before you run the
macro.

I have a workbook with 95 worksheets in it, and I'd like to be able to split
these out into individual workbooks. I looked on Ron DeBruin's site and got

[quoted text clipped - 99 lines]
--
Message posted via http://www.officekb.com



--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200912/1

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split workbook into multiple workbooks Joe_Hunt via OfficeKB.com Excel Programming 0 December 10th 09 04:40 PM
Split Workbook into Multiple Workbooks jackie Excel Programming 5 November 13th 09 06:42 PM
Split data from 1 workbook into multiple workbooks based on criter bUncE Excel Worksheet Functions 0 October 29th 07 03:26 PM
Combine multiple workbooks into 1 workbook w/ multiple worksheets buffgirl71 Excel Discussion (Misc queries) 2 May 12th 06 10:30 PM
Can I split worksheets from one workbook into individual workbooks Rosana Excel Discussion (Misc queries) 0 September 19th 05 08:03 PM


All times are GMT +1. The time now is 07:31 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"