1 week ago I still in a wonderful city for business trip, that‘s a great trip I been through so far. So great for the city and my colleagues, I still remember it from time to time~
Ok, today I inadvertently saw some posts regarding to split excel sheets to workbooks, I thought someday I will face the same situation in future from others. So, I gave it a shot to achieve in powershell and VBA, as a solution for my future customers.
Powershell
First of all, we have a xlsx file contains several sheets, our goal here is to save each sheet into a workbook.
To access excel in script, the most way I used is COM object. So let‘s create one, of course set alert as false will release us from many excel alerts.
$Excel = New-Object -ComObject Excel.Application $Excel.DisplayAlerts = $false
this basicly means I opened a excel.exe, if I want to see it, there is a property $Excel.Visible, set it to true you will see.
then, I use below code to open target excel file, beaware the "Workbooks.Open()" method only accept full path.
$WorkBook = $Excel.Workbooks.Open("$PWD\all.xlsx")
Now $WorkBook.Sheets contains all sheets, all we need to do is loop each sheet and setup a new workbook to copy original sheet.
$WorkBook.Sheets | %{ # Setup new workbook path and name $NewWorkBookPath = "$PWD\$($WorkBook.Name)_$($_.Name).xlsx" # add new workbook in excel $NewWorkBook = $Excel.Workbooks.Add() # copy sheet to the new workbook $_.Copy($NewWorkBook.Sheets.Item(1)) # new workbook always have 3 blank sheets by default, below code to remove them 2..$NewWorkBook.Sheets.Count | %{ $NewWorkBook.Sheets.Item(2).Delete() } # save the new workbook to file $NewWorkBook.SaveAs($NewWorkBookPath) # close new workbook $NewWorkBook.Close() }
At last, close old workbook and excel.
$WorkBook.Close() $Excel.Quit()
But, things not over yet, some might notice there is a excel.exe process still stays in task manager even we closed powershell. in other programme languages will encounter the same problem, the way to quiet excel.exe is described in https://technet.microsoft.com/en-us/library/ff730962.aspx
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
Til now all jobs done, more developments to make the script better is not in the scope, so finished by now.
VBA
Another way to achieve the goal is VBA, open a excel and press Alt + F11 you can open VBA editor, if you can‘t you must forgot intall it when you install office.
The way in VBA is quite same like powershell, actually just with different objects.
Sub SplitSheets() Application.DisplayAlerts = False For Each Sheet In Sheets NewWorkBookPath = ActiveWorkbook.FullName & "_" & Sheet.Name & ".xlsx" Set w = Workbooks.Add Sheet.Copy w.Sheets.Item(1) For i = 2 To w.Sheets.Count w.Sheets.Item(2).Delete Next w.SaveAs NewWorkBookPath w.Close Set w = Nothing Next End Sub
It‘s just a macro, in workbook view use Alt + F8 can quick invoke macro commands, in the VBA editor view, just press F5.
- Larry
[Powershell / VBA] To split Excel sheets to individual workbooks.
原文:http://www.cnblogs.com/LarryAtCNBlog/p/4441201.html