以下代碼用於從源複製工作表,然後重命名並放入目標。複製後重命名工作表並創建主工作簿
我想擴展功能以使用另一個單元格引用來重新命名新創建的文件中的工作表名稱。 (注意,每個複製的工作簿只有一張工作表。)然後,在複製,重命名和重命名工作表後,將目標路徑中的所有工作簿合併爲一個工作簿。
Sub CopyRenameFile()
Dim src As String, dst As String, fl As String, f2 As String
Dim rfl As String
Dim rf2 As String
'Source directory
src = Range("B3")
'Destination directory
dst = Range("D3")
'File name
fl = Range("B6")
f2 = Range("B7")
'Rename file
rfl = Range("D6")
rf2 = Range("D7")
On Error Resume Next
FileCopy src & "\" & fl, dst & "\" & rfl
FileCopy src & "\" & f2, dst & "\" & rf2
If Err.Number <> 0 Then
MsgBox "Copy error: " & src & "\" & rfl
End If
On Error GoTo 0
Dim xL As Excel.Application
Set xL = New Excel.Application
xL.Visible = True
Dim wb As Excel.Workbook
Set wb = xL.Workbooks.Open(F6)
'In case you don't know how here are two ways to reference a sheet:
Dim sh As Excel.Worksheet
Set sh = xL.Sheets(1)
sh.Name = "TestMeOut"
'Saving and closing are important...
wb.Save
Set wb = Nothing
xL.Quit
Set xL = Nothing
End Sub
我沒有看到任何代碼複製表。這與這個問題有什麼關係? –
以上步驟從源目錄獲取文件f1,然後重命名該文件並將其放入目標目錄。我想添加到它以採取新命名的文件並更改工作表名稱。之後,列表中的所有文件都將被複制並重命名,然後將它們合併到一個單獨的工作簿中,並與正在執行宏的工作簿分開。 –