0
我正在創建一個表格,該表格從查詢生成一個表格並將數據分散到Excel文件中的7個數據表中。我遇到的問題極其不一致。這個錯誤並不總是出現,當它出現在7個輸出中的一個時。外部表格不是預期的格式(Access 2010 VBA)
我的代碼:
Private Sub cmdExport_Click()
Dim xl As Object
'Step 1: Start Excel, then open the target workbook.
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open (CurrentProject.Path & "\" & "Report.xlsm")
'Step 2: Make Excel visible
xl.Visible = True
xl.ActiveWorkbook.Activate
'Step 3: Run the target macro
xl.Run "PreImport"
' xl.ActiveWorkbook.Save
'Step 4: Close and save the workbook, then close Excel
xl.ActiveWorkbook.Close
xl.Quit
'Step 5: Memory Clean up.
Set xl = Nothing
' The TransferSpreadsheet command below this comment is where the errors are occuring
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_1", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_1"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_2", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_2"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_3", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_3"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_4", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_4"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_5", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_5"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_6", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_6"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_7", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_7"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_8", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_8"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_9", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_9"
' The TransferSpreadsheet command above this comment is where the errors are occuring
Dim xlapp As Excel.Application
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
xlapp.Workbooks.Open CurrentProject.Path & "\" & "Report.xlsm", True, False
Set xlapp = Nothing
End Sub
宏 「預導入」 正被RAN執行以下操作:
- 拷貝/粘貼2列到新的位置(顯示上週的數據)。
- 刪除從
WORKSHEET_NAME_1
所有的工作表WORKSHEET_NAME_9
- 保存文件
主代碼(如上所示)被認爲:
- 運行
PreImport
宏 - 轉儲數據到
Report.xlsm
- 保存文件
哪個部分你看作是一個範圍?我不 ' –