我有一些vba代码,我循环遍历模板文件目录,打开任何具有.xlsx扩展名的文件并复制到wbTarget工作簿"报告"每个工作簿中的选项卡。我可以复制工作表,但我想更改已复制到变量Filename
的选项卡的名称。
Public Sub CopySheets()
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Path = "C:\combine\"
Filename = Dir(Path & "*.xlsx")
Set wbTarget = ActiveWorkbook
Do While Len(Filename) > 0
Set wbk = Workbooks.Open(Path & Filename)
Sheet = 2
wbk.Sheets("Reporting").Copy After:=Workbooks("Combined.xlsm").Sheets(Workbooks("Combined.xlsm").Sheets.Count)
'>>change name of copied tab here<<
wbk.Close True
Filename = Dir
Sheet = Sheet + 1
Loop
End Sub
答案 0 :(得分:2)
此行之后
wbk.Sheets("Reporting").Copy After:=Workbooks("Combined.xlsm").Sheets(Workbooks("Combined.xlsm").Sheets.Count)
尝试
ActiveSheet.Name = "myname" '<== replace with whatever name you want to assign to the tab
答案 1 :(得分:0)
此行之后
`wbk.Sheets("Reporting").Copy After:=Workbooks("Combined.xlsm").Sheets(Workbooks("Combined.xlsm").Sheets.Count) `
试试这个
`Workbooks("Combined.xlsm").Sheets(Workbooks("Combined.xlsm").Sheets.Count-1) = "NewName"`