gpt4 book ai didi

vba - Excel 2003 VBA : Move a sheet into a new workbook that is referenced by a variable

转载 作者:行者123 更新时间:2023-12-03 00:05:47 25 4
gpt4 key购买 nike

我有一个函数,用于运行数据透视表的 ShowPages() 命令,然后将每个工作表保存到单独的文件中。

这是我希望能够做到的方式:

Sub Split()
ThisWorkbook.Sheets("Data").PivotTables("Data").ShowPages PageField:="Codename"
Dim newWb As Workbook

For Each s In ThisWorkbook.Sheets
If s.Name <> "Data" Then
Set newWb = s.Move #This is the line I'm trying to work out
newWb.SaveAs Filename:="C:\Export\" + s.Name + ".xls"
newWb.Close
End If
Next s

End Sub

不幸的是,这遇到了一系列与没有创建对象等相关的问题(可以理解)。最明智的方法是什么?

最佳答案

Sub Split()
ThisWorkbook.Sheets("Data").PivotTables("Data").ShowPages PageField:="Codename"
Dim newWb As Workbook

For Each s In ThisWorkbook.Sheets
If s.Name <> "Data" Then
''Added by Soldieraman
Dim sheetName As String
sheetName = s.Name

Set newWb = Workbooks.Add
s.Move before:=newWb.Sheets(1)
Application.DisplayAlerts = False
newWb.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete
Application.DisplayAlerts = True

''Edited by soldieraman
newWb.SaveAs Filename:="C:\Export\Test" & sheetName & ".xls"
newWb.Close
End If
Next s
End Sub

关于vba - Excel 2003 VBA : Move a sheet into a new workbook that is referenced by a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3332941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com