Jump to content

Saving multiple worksheets of a workbook as seperate workbooks


Recommended Posts

  • Moderators

@Rajat231 As with most things AutoIt, there are multiple ways to skin the proverbial cat. For what you're after, you could use a simple for loop. For example, given a workbook with a Sheet for every day of the week Sunday through Saturday, this snippet works just fine.

#include <Excel.au3>

$oExcel = _Excel_Open(False)
$oWorkbook = _Excel_BookOpen($oExcel, @DesktopDir & "\Days.xlsx")
$aList = _Excel_SheetList($oWorkbook)

For $a = 1 To UBound($aList) - 1
    $newWorkBook = _Excel_BookNew($oExcel, 1)
    _Excel_SheetCopyMove($oWorkbook, $aList[$a][0], $newWorkBook)
    _Excel_BookSaveAs($newWorkBook, @DesktopDir & "\" & $aList[$a][0] & ".xlsx")
    _Excel_BookClose($newWorkBook)
Next

_Excel_BookClose($oWorkbook)
_Excel_Close($oExcel)

You can choose for yourself whether you want to leave as-is, where the sheets are just copied into their own workbooks, or set the last parameter of _Excel_SheetCopyMove to False, so they are actually moved to their own workbooks. Just be aware that if you choose to move them, Excel will not move the last sheet (can't have a workbook with no sheets), so you'll have to add in some logic to simply rename the original workbook once all the other sheets are gone.

Play around with the example and try to fit it to your needs. If you have any questions, please post back here.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...