寻求支持编辑以下代码,使其重命名所有工作表(excel),类似于文件夹(循环)中的工作簿名称。如果工作簿有多个工作表,则将其重命名为workbookname(1),workbookname(2)等。
Sub EditSheetName()
Dim NewName
NewName = Replace(ActiveWorkbook. Name, ".xl*", "")
ActiveSheet.Select
ActiveSheet.Name = NewName
End Sub
答案 0 :(得分:0)
也许只是一个简单的循环: 我还没想出如何使用通配符替换“.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
答案 1 :(得分:0)
对于多个工作表,您需要一个if语句:
Dim s As String, i as Integer
If Sheets.Count=1 Then
s = Replace(ActiveWorkbook.Name,".xlsx","")
ActiveSheet.Name = s
Else
For i = 1 to Sheets.Count
s = Replace(ActiveWorkbook.Name,".xlsx","")
Sheets(i).Name = s & "(" & i & ")"
Next i
End If
我这样做,所以(#)只显示多个。如果你不在乎,你只需要在Else部分进行循环。