0
這正是我想要的,但是在更改文件夾路徑文件名等等以滿足我的需要後,它不起作用。將數據從一個工作簿複製到特定文件夾中的多個工作簿的宏
- 我的工作簿的路徑是C:\用戶\ navinc \桌面\
- 我的文件名是測試(2)
- 我的工作表名是碩士項目列表中的文件夾路徑C
- 我的目標文件:\ Users \ navinc \ Desktop \ test \
- 錯誤消息:「下標超出範圍」有什麼我失蹤?
我的代碼:
Sub Macro1()
Dim file As String
Dim myPath As String
Dim wb As Workbook
Dim rng As Range
Dim wbMaster As Workbook
'if master workbook already opened
'Set wbMaster = Workbooks("test(2).xlsx")
'if master workbook is not opened
Set wbMaster = Workbooks.Open("C:\Users\navinc\Desktop\test(2).xlsx")
Set rng = wbMaster.Sheets("Master Project list").Range("A1:D1")
myPath = "C:\Users\navinc\Desktop\test\" ' note there is a back slash in the end
file = Dir(myPath & "*.xlsx*")
While (file <> "")
Set wb = Workbooks.Open(myPath & file)
rng.Copy
With wb.Worksheets("Master Project list").Range("A1")
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
End With
wb.Close SaveChanges:=True
Set wb = Nothing
file = Dir
Wend
Application.CutCopyMode = False
End Sub
總是有益的提及*哪一行*給出了錯誤... –
這行錯誤使用wb.Worksheets(「MasterProjectlist」)。範圍(「A1」)的事情是,taget文件將有不同的表名。 – Navin
似乎'wb'沒有名爲「主項目列表」的表單?如果你不知道目標表名是什麼,那麼除非(例如)它總是你想要的第一個工作表,在這種情況下你可以使用'With wb.Worksheets(1).Range(「A1 「)' –