Excel进程未关闭,代码每隔一秒运行一次

时间:2014-12-10 06:30:24

标签: excel vba excel-vba access-vba

我有一些命令按钮可以将访问表发送到Excel电子表格并进行一些格式化并在其中输入一些公式。其他命令按钮可以工作,但是这个按钮会落在LastRowInventory行。

我确信它与oBook有关,但我无法弄清楚如何解决它。我认为它是因为它试图获得它已经拥有的对象。它每隔一次运行顺利,但不会关闭excel进程。我在过去几个小时内解决这个问题的尝试都没有奏效。

我得到的错误如下:

Run-time error '462': The remote server machine does not exist or is unavailable

感谢任何帮助。我相信这是一个简单的修复,但是我不太懂,我对编程很新。代码如下。

Private Sub INVENTORYLIST_Click()

DTable = InputBox("Input Table Name")
'****************************TRANSFER TO EXCEL********************************
Dim strWorksheetPathTable As String
    strWorksheetPathTable = "O:\GData\Downstream_LNG\Data Mgmt\CEDA\Reports\" & DTable & "\" & DTable & ".xls"

DoCmd.TransferSpreadsheet transfertype:=acExport, _
    spreadsheettype:=acSpreadsheetTypeExcel12, _
    TableName:=("" & DTable & "_INVENTORY LIST"), FileName:=strWorksheetPathTable, _
    hasfieldnames:=True, _
    Range:="InventoryList"


'****************************FORMAT INVENTORY SHEET***********************************
Dim xlApp As Object
Dim xlWB As Object
Set xlApp = CreateObject("Excel.Application")
Dim oBook As Excel.Workbook
Dim InventoryListSheet As Excel.Worksheet
Dim SummarySheet As Excel.Worksheet

Set xlWB = xlApp.Workbooks.Open("" & strWorksheetPathTable & "")
Set oBook = GetObject("" & strWorksheetPathTable & "")
Set InventoryListSheet = oBook.Sheets("InventoryList")
Set SummarySheet = oBook.Sheets("Summary")


With xlWB
    With InventoryListSheet
 'Some Spreadesheet Formatting in here
    End With
End With


'****************************CREATE OE STATUS BREAKDOWN CALCULATIONS ON SUMMARY SHEET**********************
Dim LastRowInventory As Long
LastRowInventory = oBook.Sheets("InventoryList").Range("A" & Rows.Count & "").End(xlUp).Row

With xlWB
    With SummarySheet
'Some Spreadsheet Formulas here
    End With
End With

'*********************************ORDER WORKSHEETS*************************************
With xlWB
    .Sheets("InventoryList").Select
    .Sheets("InventoryList").Move Before:=oBook.Sheets(1)
    .Sheets("Summary").Select
    .Sheets("Summary").Move Before:=oBook.Sheets(1)
End With

If Not SummarySheet Is Nothing Then
    Set SummarySheet = Nothing
End If
If Not InventoryListSheet Is Nothing Then
    Set InventoryListSheet = Nothing
End If
If Not oBook Is Nothing Then
    Set oBook = Nothing
End If
If Not xlWB Is Nothing Then
    xlWB.Save
    xlWB.Close
    Set xlWB = Nothing
End If
If Not xlApp Is Nothing Then
    xlApp.Quit
    Set xlApp = Nothing
End If

DoCmd.SetWarnings True

MsgBox ("INVENTORY SHEET HAS BEEN CREATED.")

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:

LastRowInventory = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row

如果这不起作用,请尝试:

LastRowInventory = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

这对你有帮助吗?

修改

LastRowInventory = InventoryListSheet.Range("A" & InventoryListSheet.Rows.Count & "").End(xlUp).Row

通过指定要在该问题上计算行的工作表是固定的。