我有以下脚本 - 它应该导出访问表" vrt_master"进入Excel表格,进行一些格式化,然后关闭文件。 第一次脚本运行没有问题,但当我尝试重新运行它时,我收到错误:"运行时错误70:权限被拒绝"当它试图删除旧文件(参见附加代码)和"运行时错误1004:方法"单元"对象" _Global"失败"
Excel表格无法正常关闭(即使文件已关闭,我也可以在任务管理器中找到一个或多个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
&#39; (上图)这是我得到错误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
&#39; (上图)这是我得到运行时错误&#39; 1004&#39; - 方法&#39;范围&#39;对象&#39; _Global&#39;失败
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"
&#39; (上图)这是我得到的运行时错误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
我会非常感谢任何帮助, 阿尔文
答案 0 :(得分:1)
这应该可以解决孤立进程的问题:
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