我正在编写一个简单的宏,它将打开、保存和关闭一个 Excel 文件,例如 myworkbook.xlsx,但我无法执行此操作。我的文件 myworkbook.xlsx 位于:C:\Users\Administrator\Desktop\Temp\myworkbook.xlsx
我已经写了代码
Sub openSaveClose()
Workbooks.Open "C:\Users\Administrator\Desktop\Temp\files\myworkbook.xlsx" // This is working fine
Workbooks("C:\Users\Administrator\Desktop\Temp\myworkbook.xlsx").Save
End sub
错误是:
最佳答案
打开文件时只需要指定路径即可:
Sub openSaveClose()
Workbooks.Open "C:\Users\Administrator\Desktop\Temp\files\myworkbook.xlsx"
Workbooks("myworkbook.xlsx").Save
Workbooks("myworkbook.xlsx").Close
End sub
使用对象变量,您可以执行以下操作(然后无需在 Save/Close 语句中使用工作簿的名称):
Sub openSaveClose()
Dim wb as Workbook
Set wb = Workbooks.Open("C:\Users\Administrator\Desktop\Temp\files\myworkbook.xlsx")
wb.Save
wb.Close
End sub
关于vba - 打开、保存和关闭任何文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33596786/