这正是我想要的,但在根据我的需要更改文件夹路径文件名等后,它无法正常工作。
我的代码:
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
答案 0 :(得分:0)
似乎if-else
没有名为“主项目列表”的工作表?
如果您不知道目标表名称是什么,那么这将是棘手的,除非(例如)它始终是您想要的第一个工作表,在这种情况下您可以使用:
switch