我正在编写一个程序,在其中打开 Excel 文件并将其迁移到另一个主文件中。之后我想删除它。当我尝试删除文件时,出现权限被拒绝错误。
在线研究后,我确实看到我犯了一些错误,然后确保正确关闭文件并销毁相关对象,但我仍然收到错误消息。
Sub Test()
Dim app as New Excel.Application
Dim wb as Workbook
Set wb.app.Workbooks.Open(FILEPATH & "\filename.csv")
'I know the filepath is correct as I can open and manipulate the file
'... Do things
wb.close
Set wb = Nothing
kill FILEPATH & "\filename.csv"
'I plan on adding error handling once the basic function works
End Sub
运行时错误“70”:权限被拒绝
编辑:在调试后我可以说我可以成功销毁对象,但是 Excel 进程在后台保持打开状态,我必须在任务管理器中手动杀死它才能使代码正常工作。
EDIT2:我解决了我的问题。我想通过 VB 脚本在 Excel 之外运行这个程序。我完成了它,之前没有打开过 Excel。一切正常删除。
这是您的代码示例,对我来说效果很好:
Sub Test()
Dim app As New Excel.Application
Dim wb As Workbook
Dim path As String
path = "your path"
Set wb = app.Workbooks.Open(path & "\dati.xlsx")
'I know the filepath is correct as I can open and manipulate the file
'... Do things
wb.Sheets(1).Range("A1:E20").Copy
ThisWorkbook.Worksheets("Sheet1").Range("A10").PasteSpecial xlPasteValues
wb.Close
Set wb = Nothing
Kill path & "\dati.xlsx"
'I plan on adding error handling once the basic function works
End Sub
我已经创建了文件 dati.xlsx.I 从 sheet1(文件 data.xlsx)复制范围 A1:E20 并将数据粘贴到 sheet1(工作表中的宏)。结果是数据和删除文件data.xlsx
当你写 Set wb.app 时,在你的邮政编码中支付 Attantion ......有一个错误......
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句