Method 'Close' of object '_Workbook' failed

MrNod

New Member
Joined
Jul 24, 2014
Messages
6
Hey everyone,

I'm using an ActiveX Button to trigger a macro which import a sheet of a workbook in my current workbook. Until here no problem, I'm using my GetOpenFilename method, set as a wb, open, move, etc..

But then come the close method! (na na na naaaa)

Run-time error '-2147221080 (800401a8)':
Method 'Close' of object '_Workbook' failed

So it make this error, but, if I don't pay attention to it [On Error Resume Next & On Error Goto 0], everything work well!

I looked up in few similar thread, but even if I use this exact same code, the error still happen.

So here's my code :

Code:
'Import file
Dim ImportFile As String
ImportFile = Application.GetOpenFilename("Microsoft Excel Worksheet (*.xlsx),*.xslx")


'------DisplayAlert--Stop--&--ScreenUpdating--Stop--------
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'---------------To--make--it--smoother--------------------


'Check if Import file already exist in the current WB
Dim Sh As Worksheet

On Error Resume Next
Set Sh = Worksheets("Forecast")
On Error GoTo 0
If Not Sh Is Nothing Then
    Sh.Delete
End If

'Open Import file

If ImportFile = "False" Then Exit Sub

Dim wb As Workbook

Set wb = Workbooks.Open(ImportFile, True)
Sheets.Select
ActiveSheet.Name = "Forecast"
Sheets.Move After:=Workbooks("Final Project.xlsm").Sheets(1)

'Close the workbooks of ImportFile
On Error Resume Next '[I don't know why, The Close method is working but trigger a error]
                     '[even with DisplayAlerts = False I must use On error to make it work..]
wb.Close savechanges:=False
On Error GoTo 0

Thanks you for taking time to help!

EDIT: I'm not using userform
 
Last edited:
Is there somebody who have an idea where I can find an answer? Maybe just a little advice please?
 
Upvote 0
Code:
Dim wb As Workbook

Set wb = workbooks.Open(ImportFile, True)
Sheets.Select
ActiveSheet.name = "Forecast"
Sheets.Move After:=workbooks("Final Project.xlsm").Sheets(1)


'Close the workbooks of ImportFile
On Error Resume Next
wb.Close savechanges:=False
On Error GoTo 0

Just step back for a minute and read your code.

Code:
Set wb = workbooks.Open(ImportFile, True)
Code:
wb.Close savechanges:=False

This is what your code is saying:
Code:
workbooks.Open(ImportFile,True).Close savechanges:=False

That's not going to work.
 
Upvote 0
Really? -.-'

I read it si much time and i didnt sée it... (There's more code)

Thanks a lot!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top