VBA脚本将工作簿中的所有工作表拆分为单独的文件

时间:2017-05-24 16:34:36

标签: excel vba excel-vba

我有一个脚本为工作簿中的每个工作表执行vlookup,然后将每个工作表拆分为自己的文件。我有下面的脚本,但它不起作用。 vlookup部分工作正常,但我遇到了分裂的问题。它没有失败并给我一个错误,它只是没有做任何事情。

Sub Splitbook()
MyPath = "***Folder Location***"
For Each sht In Workbooks("PO135 Division 1.xlsx").Worksheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xlsx"
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub

我需要拆分文件,然后将它们保存在一个不同的文件夹中(“ 文件夹位置 ”) - 这只是暂时的占位符,它将在运行脚本之前更新 -

有什么想法?感谢帮助!

1 个答案:

答案 0 :(得分:0)

将它放在常规模块中:

Sub NewWb()
Dim ws As Worksheet
Dim wbActive As Workbook
Dim wbNew As Workbook
Dim x As Single
Application.ScreenUpdating = False
    Set wbActive = ActiveWorkbook

    For Each ws In wbActive.Worksheets
        Set wbNew = Workbooks.Add
        ws.Copy Before:=wbNew.Sheets(1)
        abc = "C:\Files\" & ws.Name & ".xlsx"
        Application.DisplayAlerts = False
        wbNew.Sheets("Sheet1").Delete
        Application.DisplayAlerts = True
        wbNew.SaveAs Filename:=abc
        wbNew.Close Saved = True
    Next ws
    Set ws = Nothing
    Set wbActive = Nothing
    Set wbNew = Nothing
    Application.ScreenUpdating = True    
End Sub