我正在尝试在目录中的所有Excel工作表上运行宏,并保存在新目录中。我的代码如下:
Sub TestMacro()
Dim pathname As String
Dim newpath As String
Dim newfullpath As String
Dim filenam As String
Dim fullpath As String
pathname = "\\filepath\"
newpath = "\\filepath\newfilepath\"
filenam = Dir(pathname & "*.xls")
fullpath = pathname & filenam
Do While filenam <> ""
MsgBox (filenam)
newfullpath = newpath & filenam
Workbooks.Open Filename:=fullpath, ReadOnly:=True
Sheets("Sheet 1").Select
Sheets("Sheet 1").Name = "Sheet1"
ActiveWorkbook.SaveAs Filename:=newfullpath
'MsgBox (filenam)
ActiveWindow.Close
'Workbooks.Close
filenam = Dir()
Loop
End Sub
这里的问题是新目录中的所有Excel工作表都包含\ filepath的第一个Excel工作表的内容 我在哪里错误的代码? 在此先感谢!!
答案 0 :(得分:1)
您的代码不断打开同一个工作簿:
Workbooks.Open Filename:=fullpath, ReadOnly:=True
您应该打开由pathname & filenam
定义的工作簿,即
Workbooks.Open Filename:=pathname & filenam, ReadOnly:=True