我正在尝试制作一个可以打开多个工作簿(也只有一个)的 VBA,将它们的所有工作表复制到另一个工作簿中。我想直接从 PersonalWorkbook 使我的代码正常工作,以便我可以在我想要的任何新工作簿中使用它。
我知道这不是很多,但我被这些不完整的版本困住了(第二个根本没有按预期工作)......
Sub conso()
Dim folderpath As String
Dim file As String
Dim i As Long
folderpath = InputBox("Please paste the folder path", "Choose Folder") & "\"
file = Dir(folderpath)
Do While file <> ""
Workbooks.Open folderpath & file
ActiveWorkbook.Worksheets(1).Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
'ActiveSheet.Name = Right(Left(file, Len(file) - 5), Len(Left(file, Len(file) - 5)) - InStr(1, Left(file, Len(file) - 5), "("))
'ActiveSheet.Name = file
ActiveSheet.Name = Left(file, InStr(file, ".") - 1)
Workbooks(file).Close
file = Dir()
Loop
End Sub
第二:
Sub open_and_copy_sheets()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim my_FileName As Variant
Dim nm As String
Dim nm2 As String
Dim i As Integer
nm = ActiveWorkbook.Name
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")
If my_FileName <> False Then
Workbooks.Open Filename:=my_FileName
End If
Workbooks(Workbooks.Count).Activate
nm2 = ActiveWorkbook.Name
For i = 1 To Workbooks(nm2).Worksheets.Count
Sheets(i).Copy after:=Workbooks(nm).Sheets(Workbooks(nm).Sheets.Count)
Next i
Workbooks(nm2).Close SaveChanges:=False
Workbooks(nm).Activate
Worksheets(1).Activate
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
任何帮助将不胜感激!我在 vba 方面不太好,所以也欢迎任何解释:)