2014-07-09 161 views
0

即時通訊程序絕對是初學者。我在VBA寫一些宏。我想用excel應用程序將一些參數加載到我的變量中。在這個宏Excel仍然運行的過程結束後,我有問題,我不明白爲什麼。在我的宏中,我關閉了excel,但在這個Execel仍在運行之後。 你能幫我解決這個問題嗎?非常感謝關閉Excel VBA

Global Machine_ID As Integer 
Public Sub cteni_z_xls() 
Dim excel As excel.Application 
Dim xlBook As excel.Workbook 
Dim xlSheet As excel.Sheets 

Set xlBook = GetObject("D:\NEXNET\Zakázky\Kompan\VBA_07072014\PP\Nastroje.xls") 
Dim AC_ID As Integer 
'Dim Machine_ID As Integer 
'přečtu excel 
xlBook.Application.ScreenUpdating = False 
Set xlBook = Workbooks.Open("D:\NEXNET\Zakázky\Kompan\VBA_07072014\PP\Nastroje.xls", True, True) 


    If Tool_ID = 1 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B4").Value 
    head = xlBook.Worksheets(1).Range("C4").Value 
    ElseIf Tool_ID = 2 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B5").Value 
    head = xlBook.Worksheets(1).Range("C5").Value 
    ElseIf Tool_ID = 3 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B6").Value 
    head = xlBook.Worksheets(1).Range("C6").Value 
    ElseIf Tool_ID = 4 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B7").Value 
    head = xlBook.Worksheets(1).Range("C7").Value 
    ElseIf Tool_ID = 5 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B8").Value 
    head = xlBook.Worksheets(1).Range("C8").Value 
    ElseIf Tool_ID = 6 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B9").Value 
    head = xlBook.Worksheets(1).Range("C9").Value 
    ElseIf Tool_ID = 7 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B10").Value 
    head = xlBook.Worksheets(1).Range("C10").Value 
    ElseIf Tool_ID = 8 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B11").Value 
    head = xlBook.Worksheets(1).Range("C11").Value 
    ElseIf Tool_ID = 9 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B12").Value 
    head = xlBook.Worksheets(1).Range("C12").Value 
    ElseIf Tool_ID = 10 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B13").Value 
    head = xlBook.Worksheets(1).Range("C13").Value 
    ElseIf Tool_ID = 11 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B14").Value 
    head = xlBook.Worksheets(1).Range("C14").Value 
    ElseIf Tool_ID = 12 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B15").Value 
    head = xlBook.Worksheets(1).Range("C15").Value 
    ElseIf Tool_ID = 13 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B16").Value 
    head = xlBook.Worksheets(1).Range("C16").Value 
    ElseIf Tool_ID = 14 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B20").Value 
    head = xlBook.Worksheets(1).Range("C20").Value 
    ElseIf Tool_ID = 15 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B21").Value 
    head = xlBook.Worksheets(1).Range("C21").Value 
    ElseIf Tool_ID = 16 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B22").Value 
    head = xlBook.Worksheets(1).Range("C22").Value 
    ElseIf Tool_ID = 17 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B23").Value 
    head = xlBook.Worksheets(1).Range("C23").Value 
    ElseIf Tool_ID = 18 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B24").Value 
    head = xlBook.Worksheets(1).Range("C24").Value 
    ElseIf Tool_ID = 19 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B25").Value 
    head = xlBook.Worksheets(1).Range("C25").Value 
    ElseIf Tool_ID = 20 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B26").Value 
    head = xlBook.Worksheets(1).Range("C26").Value 
    ElseIf Tool_ID = 21 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B27").Value 
    head = xlBook.Worksheets(1).Range("C27").Value 
    ElseIf Tool_ID = 22 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B28").Value 
    head = xlBook.Worksheets(1).Range("C28").Value 
    ElseIf Tool_ID = 23 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B29").Value 
    head = xlBook.Worksheets(1).Range("C29").Value 
    ElseIf Tool_ID = 24 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B30").Value 
    head = xlBook.Worksheets(1).Range("C30").Value 
    ElseIf Tool_ID = 25 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B31").Value 
    head = xlBook.Worksheets(1).Range("C31").Value 
    ElseIf Tool_ID = 26 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B32").Value 
    head = xlBook.Worksheets(1).Range("C32").Value 
    ElseIf Tool_ID = 27 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B33").Value 
    head = xlBook.Worksheets(1).Range("C33").Value 
    ElseIf Tool_ID = 28 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B34").Value 
    head = xlBook.Worksheets(1).Range("C34").Value 
    ElseIf Tool_ID = 29 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B35").Value 
    head = xlBook.Worksheets(1).Range("C35").Value 
    ElseIf Tool_ID = 30 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B36").Value 
    head = xlBook.Worksheets(1).Range("C36").Value 
    ElseIf Tool_ID = 31 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B37").Value 
    head = xlBook.Worksheets(1).Range("C37").Value 
    ElseIf Tool_ID = 32 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B38").Value 
    head = xlBook.Worksheets(1).Range("C38").Value 
    ElseIf Tool_ID = 33 Then 
    Machine_ID = xlBook.Worksheets(1).Range("B39").Value 
    head = xlBook.Worksheets(1).Range("C39").Value 

End If 



KillExcel 
End Sub 
Public Sub KillExcel() 
'zabiju excel ElseIf Tool_ID = 4 Then 
' Machine_ID = xlBook.Worksheets(1).Range("B6").Value 
    On Error Resume Next 
    If Not xlBook Is Nothing Then 
    xlBook.Quit 
' Application.Quit 
    Set xlBook = Nothing 
    End If 

'xlBook.Application.Quit 

'close active woork book 

ActiveWorkbook.Close SaveChanges:=False 
End Sub 

回答

0

您的xlbook變量不在KillExcel過程的範圍內。你可以使用這樣的事情(也冷凝代碼位):

Global Machine_ID As Integer 
Public Sub cteni_z_xls() 
    Dim xlapp As Excel.Application 
    Dim xlBook As Excel.Workbook 

    Set xlBook = GetObject("D:\NEXNET\Zakázky\Kompan\VBA_07072014\PP\Nastroje.xls") 
    Dim AC_ID As Integer 
    'Dim Machine_ID As Integer 
    'prectu excel 
    Set xlapp = xlBook.Application 
    xlapp.ScreenUpdating = False 
    Set xlBook = Workbooks.Open("D:\NEXNET\Zakázky\Kompan\VBA_07072014\PP\Nastroje.xls", True, True) 

    With xlBook.Worksheets(1) 
     Select Case tool_ID 
      Case 1 To 13 
       Machine_ID = .Cells(tool_ID + 3, "B").Value 
       head = .Cells(tool_ID + 3, "C").Value 
      Case 14 To 33 
       Machine_ID = .Cells(tool_ID + 6, "B").Value 
       head = .Cells(tool_ID + 6, "C").Value 
     End Select 
    End With 

    xlBook.Close False 
    xlapp.Quit 

End Sub 
0

從你的腳本,做如下修改

... 
KillExcel(xlBook) 
End Sub 
Public Sub KillExcel(w as workbook) 
'zabiju excel ElseIf Tool_ID = 4 Then 
' Machine_ID = w.Worksheets(1).Range("B6").Value 
    On Error Resume Next 
    If Not w Is Nothing Then 
    w.Quit 
' Application.Quit 
    Set w = Nothing 
    End If 

'w.Application.Quit 

'close active woork book 

ActiveWorkbook.Close SaveChanges:=False 
End Sub 

從這個腳本,如預期腳本應該運行。

乾杯

帕斯卡爾

multiskillz