你好,我正试图从我的工作簿中保存多个工作表,并将其保存在一个新的文件中的过滤数据,它将完成整个分发列表。所以我做了过滤器功能,它可以过滤和下载按钮,但它只适用于最后一张表和未过滤的数据。
下面是我的代码以供参考:
Sub ExportXLSM()
Dim myWorksheets() As String 'Array to hold worksheet names to copy
Dim newWB As Workbook
Dim CurrWB As Workbook
Set CurrWB = ThisWorkbook
Dim userpath As String
userpath = Environ("UserProfile")
Dim sToday As String
Dim s11 As Worksheet
Set s11 = ThisWorkbook.Sheets("Monthly_Budget")
Dim MyPath As String
Dim MyFileName As String
sToday = s11.Range("A7").Value
myWorksheets = Split("Summary, Monthly_Budget, History_1", ",")
MyFileName = "KIG_BUDGET_2024_" & sToday & ".xlsm"
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the location and click ok!"
.AllowMultiSelect = False
.InitialFileName = userpath & " \ Desktop \ "
'<~~ The start folder path for the file picker
If .Show <> -1 Then GoTo NextCode
MyPath = .SelectedItems(1) & "\"
End With
NextCode:
Dim i As Integer
For i = LBound(myWorksheets) To UBound(myWorksheets) 'Go through entire array
Set newWB = Workbooks.Add 'Create new workbook
CurrWB.Sheets(Trim(myWorksheets(i))).Copy Before:=newWB.Sheets(1)
newWB.SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
newWB.Close saveChanges:=False
Next i
End Sub
1条答案
按热度按时间dced5bon1#
您正在为每个工作表创建一个新的工作簿,并覆盖以前保存的工作簿,因为它们都使用相同的名称“MyFileName”。