简体   繁体   中英

Automation error or Error 462 in excel VBA code

I get an error "-2147417848 (80010108) run time error with the following code im using in Excel VBA":

sub import()

Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False

    On Error GoTo error

    Dim App As New excel.Application 
    Dim wbImport As Workbook
    Set wbImport = App.Workbooks.Open(ThisWorkbook.Path & "\" & "example.xlsm")

       'DATA COPY
       Dim Lastrow As Integer
       Lastrow = ThisWorkbook.Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row
       ThisWorkbook.Worksheets("DATA").Range("A2:AM" & Lastrow).Copy
       wbImport.Worksheets("DATA").Range("a2").PasteSpecial Paste:=xlPasteValues 
       wbImport.Worksheets("CONFIG").Shapes("pdv").Visible = False


  wbImport.Close SaveChanges:=True 
  App.CutCopyMode = False 
  App.Quit 
  MsgBox "OK"

Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
  

error:

   App.CutCopyMode = False 
   wbImport.Close SaveChanges:=True 
   App.Quit 
    
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True

MsgBox "FAIL"

End sub

And recently found out that, by adding

Set App = Nothing    

after

 App.Quit

It gives me "Error 462", but I have no clue why. Debugging just runs normally, error prompts at the very end, after msgboxes.

I'm sure this is not the best solution but are the ones I found:

1- No need to Dim App As New excel.Application to open an excel file in the background.

Screenupdating property right after and before opening is enough to not show the process:

Application.ScreenUpdating = False

Dim wbImport As Workbook
Set wbImport = Workbooks.Open(ThisWorkbook.Path & "\example.xlsm" 

Application.ScreenUpdating = True

2- I forgot to add Exit sub right before "error:" handle.

Final code:

    sub import()
    On Error GoTo error
    
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
      
        Dim wbImport As Workbook
        Set wbImport = App.Workbooks.Open(ThisWorkbook.Path & "\" & "example.xlsm")
    
           'DATA COPY
           Dim Lastrow As Integer
           Lastrow = ThisWorkbook.Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row
           ThisWorkbook.Worksheets("DATA").Range("A2:AM" & Lastrow).Copy
           wbImport.Worksheets("DATA").Range("a2").PasteSpecial Paste:=xlPasteValues 
           wbImport.Worksheets("CONFIG").Shapes("pdv").Visible = False
    
      wbImport.Close SaveChanges:=True 
      MsgBox "OK"

Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
  
exit sub 

error:

wbImport.Close SaveChanges:=True   
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True

MsgBox "FAIL"

End sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM