VBA访问Excel导出,重新运行脚本时出现错误1004和70

我有以下脚本 – 它应该将Access表“vrt_master”导出到Excelsheet,做一些格式化,然后closures文件。 脚本第一次运行没有问题,但是当我尝试重新运行它时,它会尝试删除旧文件(请参阅后面的代码)和“运行时错误1004年:错误:运行时错误70:权限被拒绝”对象“_Global”的方法“单元格”失败。

不知何故Excelsheet不能正常closures(即使文件closures,我也可以在我的任务pipe理器中find一个或多个EXCEL.EXE进程)。

Public Sub Select_in_Excel_anzeigen() Dim sDatei As String sDatei = "C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx" If Dir(sDatei) <> "" Then Kill sDatei End If 

'(上面)这是我得到错误70

  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "vrt_master", "C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx", True, "" Dim xlApp As Excel.Application Dim xlBook As Workbook Dim xlSheet As Worksheet Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open("C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx") Set xlSheet = xlBook.Worksheets(1) ' 1. Tabellenblatt in Excel festlegen With xlSheet Dim LastColumn As Long With xlSheet LastColumn = Cells.Find(What:="*", After:=[$A1], _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column 

'(上面)这是我得到运行时错误“1004” – 对象'Global'的方法'范围'失败

 End With Dim LastRow As Long With xlSheet LastRow = Cells.Find(What:="*", After:=[A$1], _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row End With xlSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(LastRow, LastColumn)), , xlYes).Name = _ "Table1" 

'(上面)这是我得到运行时错误1004表不能重叠一个范围

 Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select xlSheet.ListObjects("Table1").TableStyle = "TableStyleLight2" Columns(LastColumn).Select Selection.Offset(0, 1).Select Range(Selection, Selection.End(xlToRight)).Select Selection.EntireColumn.Hidden = True xlSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(LastRow, 12)).Address Rows(LastRow).Select Selection.Offset(1, 0).Select Range(Selection, Selection.End(xlDown)).Select Selection.EntireRow.Hidden = True End With xlBook.Save xlBook.Close xlApp.Application.Quit Set xlApp = Nothing Set xlBook = Nothing Set xlSheet = Nothing 

我会非常感谢Alwin的帮助

这应该解决你的问题与孤立的进程:

 Public Sub Select_in_Excel_anzeigen() Dim sDatei As String Dim xlApp As Excel.Application Dim xlBook As Workbook Dim xlSheet As Worksheet Dim LastColumn As Long Dim LastRow As Long sDatei = "C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx" If Dir(sDatei) <> "" Then Kill sDatei ' (above) this is where I get Error 70 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "vrt_master", "C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx", True, "" Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open("C:\Users\a.hopf\Desktop\Export_Vertriebsreporting_Test2.xlsx") Set xlSheet = xlBook.Worksheets(1) ' 1. Tabellenblatt in Excel festlegen With xlSheet LastColumn = .Cells.Find(What:="*", After:=.Range("A1"), _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column LastRow = .Cells.Find(What:="*", After:=.Range("A1"), _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row With .ListObjects.Add(xlSrcRange, .Range(.Cells(1, 1), .Cells(LastRow, LastColumn)), , xlYes) .Name = "Table1" .TableStyle = "TableStyleLight2" End With .Range(.Cells(1, LastColumn), .Cells(1, .Columns.Count)).EntireColumn.Hidden = True .PageSetup.PrintArea = "A1:L" & LastRow .Range(.Cells(LastRow, 1), .Cells(.Rows.Count, 1)).EntireRow.Hidden = True End With xlBook.Close True xlApp.Quit Set xlApp = Nothing Set xlBook = Nothing Set xlSheet = Nothing End Sub