也許只是一個簡單的循環: 我還沒有想出如何更換「的.xlsx」使用通配符,我會以爲這將是要麼「.XLSM」或「的.xlsx」,你可以在代碼
Sub LoopThroughFolder()
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim sh As Worksheet
Dim s As String, n As String
Set Wb = ThisWorkbook
'change the address to suite
MyDir = "C:\Users\dmorrison\Downloads\TestFolderLoop\"
MyFile = Dir(MyDir & "*.xls*") 'change file extension
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0
Do While MyFile <> ""
Workbooks.Open (MyFile)
s = ActiveWorkbook.Name
n = Replace(s, ".xls", "") 'change the file extension
i = 1
For Each sh In Sheets
sh.Name = n & "(" & i & ")"
i = i + 1
Next sh
ActiveWorkbook.Close True
MyFile = Dir()
Loop
End Sub
它適用於工作簿。你也可以將這段代碼轉換成一個循環,以便它可以在一個文件夾內的所有excel文件上執行相同的宏。 – mechee69