![]() |
Split file, saving each tab/worksheet to a new workbook, w/tab asworkbook name?
I had a light-bulb moment this morning to resolve a months-long
problem ... can Excel via a script split up a file by saving each worksheet to a separate workbook and taking each new workbook's name from the sheet's original tab name? I've found a couple of somewhat similar requets he http://www.ozgrid.com/forum/showthread.php?t=14120, and he http://groups.google.ca/group/micros...17ee2c1e6a95e2, where the first one gives me an easier-looking script that seems potentially more modifiable -- at least, to my less knowledgeable eyes -- to what needs to be done <g: -------------------------------------------- Sub Movesheets() ' Macro by WillR ' www.ozgrid.com Dim oldBook As Workbook Dim newBook As Workbook Set oldBook = ActiveWorkbook Set newBook = Workbooks.Add newBook.Activate oldBook. Sheets("Sheet2").Copy after:=newBook.Sheets("Sheet2") ActiveSheet. Name = "NewSheet" ActiveWorkbook. SaveAs FileName:=Format( Date, "MMMMYY") & "Report.xls" -------------------------------------------- Of course, the difference is that this one saves only sheet 2 to a new workbook and gives it a specific name where I need all the worksheets to be saved to new, individual workbooks and to have them named by their sheet name. Thanks! :oD |
Split file, saving each tab/worksheet to a new workbook, w/tab as workbook name?
Sub Make_New_Books()
Dim w As Worksheet Application.ScreenUpdating = False Application.DisplayAlerts = False For Each w In ActiveWorkbook.Worksheets w.Copy With ActiveWorkbook .SaveAs Filename:=ActiveWorkbook.Path _ & "\" & w.Name & ".xlsx" 'or .xls .Close End With Next w Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub Gord Dibben MS Excel MVP On Thu, 27 Aug 2009 09:10:52 -0700 (PDT), StargateFanNotAtHome wrote: I had a light-bulb moment this morning to resolve a months-long problem ... can Excel via a script split up a file by saving each worksheet to a separate workbook and taking each new workbook's name from the sheet's original tab name? I've found a couple of somewhat similar requets he http://www.ozgrid.com/forum/showthread.php?t=14120, and he http://groups.google.ca/group/micros...17ee2c1e6a95e2, where the first one gives me an easier-looking script that seems potentially more modifiable -- at least, to my less knowledgeable eyes -- to what needs to be done <g: -------------------------------------------- Sub Movesheets() ' Macro by WillR ' www.ozgrid.com Dim oldBook As Workbook Dim newBook As Workbook Set oldBook = ActiveWorkbook Set newBook = Workbooks.Add newBook.Activate oldBook. Sheets("Sheet2").Copy after:=newBook.Sheets("Sheet2") ActiveSheet. Name = "NewSheet" ActiveWorkbook. SaveAs FileName:=Format( Date, "MMMMYY") & "Report.xls" -------------------------------------------- Of course, the difference is that this one saves only sheet 2 to a new workbook and gives it a specific name where I need all the worksheets to be saved to new, individual workbooks and to have them named by their sheet name. Thanks! :oD |
Split file, saving each tab/worksheet to a new workbook, w/tab asworkbook name?
Pretty freakin' amazing!!! XL is such a miracle! Yes, this seems to
have worked really, really well. I was wondering about one thing, though ... when I run the script, I got a bunch of workbooks but it's weird where they got saved. I saw the script working as "boxes" would appear on the right of the taskbar "box" for the file itself. The names would change on-the-fly from Book1, etc., to the new tab sheet name and it was pretty kewl to watch this process in action. But after, when I activated the window of the open folder where the file is located, no new workbooks. I hunted around for them and found all of them in a completely different folder. Is there a way to change ActiveWorkbook.Path _ to whatever it is that Excel would understand as being the same folder the file is in? That would then make this as close to perfect as is possible. <g Thx. :oD |
Split file, saving each tab/worksheet to a new workbook, w/tab asworkbook name?
Okay, this usually doesn't happen since I don't know the terminology
well in vb, but today I got lucky. I googled for ActiveWorkbook.Path, same as I might always do, but this time I did stumble upon an alternative, ThisWorkbook.Path and this _did_ save the new workbooks to the same folder as the original. How come the ActiveWorkbook.Path saves to our personal folder here at work I'll never know. ActiveWorkbook.Path logically sounds like it would deal with the same folder but there must be some quirk here in the office that it sends it off to my "personal" root network work folder. I also added a sort of prefix to the new workbook name so that in case the person they staffed this position with happens to do this in a folder populated with other files, the workbooks will all stay grouped together because of the "zFileSplit- " at the beginning of each file. So the slightly modified vb script looks like this now: --------------------------------------------------- Sub WORKBOOK_Split_and_new_files_named_by_tab() Dim w As Worksheet Application.ScreenUpdating = False Application.DisplayAlerts = False For Each w In ActiveWorkbook.Worksheets w.Copy With ActiveWorkbook .SaveAs Filename:=ThisWorkbook.Path _ & "\zSplitFile- " & w.Name & ".xls" 'or .xlsx; if you're using XL2007 fully and not in compatibility mode, use .xlsx instead of .xls .Close End With Next w Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub --------------------------------------------------- Thanks so much! This will make it a heck of a lot easier to manage their huge Excel file. Cheers, :oD |
Split file, saving each tab/worksheet to a new workbook, w/tab
I tried this program and it giving a runtime error '1004'; the sheet you are
copying contains cells that have more then 255 characters. When you copy the sheet only the first 255 characters in each cell will be copied. To copy all the characters, copy the cells to a new sheet instead of copying the entire sheet. When I try to debug it highlights this line: w.Copy Is there a value I can put in that will fix this problem? "StargateFanNotAtHome" wrote: Okay, this usually doesn't happen since I don't know the terminology well in vb, but today I got lucky. I googled for ActiveWorkbook.Path, same as I might always do, but this time I did stumble upon an alternative, ThisWorkbook.Path and this _did_ save the new workbooks to the same folder as the original. How come the ActiveWorkbook.Path saves to our personal folder here at work I'll never know. ActiveWorkbook.Path logically sounds like it would deal with the same folder but there must be some quirk here in the office that it sends it off to my "personal" root network work folder. I also added a sort of prefix to the new workbook name so that in case the person they staffed this position with happens to do this in a folder populated with other files, the workbooks will all stay grouped together because of the "zFileSplit- " at the beginning of each file. So the slightly modified vb script looks like this now: --------------------------------------------------- Sub WORKBOOK_Split_and_new_files_named_by_tab() Dim w As Worksheet Application.ScreenUpdating = False Application.DisplayAlerts = False For Each w In ActiveWorkbook.Worksheets w.Copy With ActiveWorkbook .SaveAs Filename:=ThisWorkbook.Path _ & "\zSplitFile- " & w.Name & ".xls" 'or .xlsx; if you're using XL2007 fully and not in compatibility mode, use .xlsx instead of .xls .Close End With Next w Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub --------------------------------------------------- Thanks so much! This will make it a heck of a lot easier to manage their huge Excel file. Cheers, :oD |
Split file, saving each tab/worksheet to a new workbook, w/tab asworkbook name?
springfield_rifles;487838 Wrote: I tried this program and it giving a runtime error '1004'; the sheet you are copying contains cells that have more then 255 characters. When you copy the sheet only the first 255 characters in each cell will be copied. To copy all the characters, copy the cells to a new sheet instead of copying the entire sheet. When I try to debug it highlights this line: w.Copy Is there a value I can put in that will fix this problem? "StargateFanNotAtHome" wrote: Okay, this usually doesn't happen since I don't know the terminology well in vb, but today I got lucky. I googled for ActiveWorkbook.Path, same as I might always do, but this time I did stumble upon an alternative, ThisWorkbook.Path and this _did_ save the new workbooks to the same folder as the original. How come the ActiveWorkbook.Path saves to our personal folder here at work I'll never know. ActiveWorkbook.Path logically sounds like it would deal with the same folder but there must be some quirk here in the office that it sends it off to my "personal" root network work folder. I also added a sort of prefix to the new workbook name so that in case the person they staffed this position with happens to do this in a folder populated with other files, the workbooks will all stay grouped together because of the "zFileSplit- " at the beginning of each file. So the slightly modified vb script looks like this now: --------------------------------------------------- Sub WORKBOOK_Split_and_new_files_named_by_tab() Dim w As Worksheet Application.ScreenUpdating = False Application.DisplayAlerts = False For Each w In ActiveWorkbook.Worksheets w.Copy With ActiveWorkbook .SaveAs Filename:=ThisWorkbook.Path _ & "\zSplitFile- " & w.Name & ".xls" 'or .xlsx; if you're using XL2007 fully and not in compatibility mode, use .xlsx instead of .xls .Close End With Next w Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub --------------------------------------------------- Thanks so much! This will make it a heck of a lot easier to manage their huge Excel file. Cheers, :oD I would like to take this a step further by doing 2 more things: (1.) I would like to save each tab/worksheet as a seperate .PDF with the tab name as the file name. (2.) Would it be possible to first (1.) and save each seperate .PDF in seprate folders in the same directory as the ActiveWorkbook where the Folder name is the same as the tab name? -- cjonz ------------------------------------------------------------------------ cjonz's Profile: http://www.thecodecage.com/forumz/member.php?userid=995 View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=129183 |
All times are GMT +1. The time now is 09:26 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com