VBA - Unable to close open workbook

njkollauf

New Member
Joined
Sep 18, 2015
Messages
12
Hello all,

My VBA Code allows the user to choose a workbook to open. First they select the wb from the directory and then that wb is opened. the issue comes when the code tries to close the wb that was just opened. There becomes an error on the red text line. Any idea why this may be? The code is simply calling a string that contains the entire wb path and file name.

Dim intChoice As Integer
Dim strPath As String

MsgBox "Next, please choose the generated calendar that you would like to compare to."
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
Cells(1, 14) = strPath
Else
strPath = 1
End If

'test if workbook exists
If Dir(strPath) <> "" Then
Workbooks.Open Filename:= _
strPath
Range("A2:L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks(wb).Activate
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks(strPath).Close
Else
MsgBox "Comparable generated calendar for this close type does not exist."
End If
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this...

Code:
    [color=green]'test if workbook exists[/color]
    [color=darkblue]If[/color] Dir(strPath) <> "" [color=darkblue]Then[/color]
        Workbooks.Open Filename:=strPath
        Range("A2:L2", Range("A2:L2").End(xlDown)).Copy
        Workbooks(wb).ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = [color=darkblue]False[/color]
        ActiveWorkbook.Close SaveChanges:=[color=darkblue]False[/color]
    [color=darkblue]Else[/color]
        MsgBox "Comparable generated calendar for this close type does not exist."
    [color=darkblue]End[/color] [color=darkblue]If[/color]
 
Last edited:
Upvote 0
Try this...

Code:
    [COLOR=green]'test if workbook exists[/COLOR]
    [COLOR=darkblue]If[/COLOR] Dir(strPath) <> "" [COLOR=darkblue]Then[/COLOR]
        Workbooks.Open Filename:=strPath
        Range("A2:L2", Range("A2:L2").End(xlDown)).Copy
        Workbooks(wb).ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = [COLOR=darkblue]False[/COLOR]
        ActiveWorkbook.Close SaveChanges:=[COLOR=darkblue]False[/COLOR]
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Comparable generated calendar for this close type does not exist."
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]

Works like a dream. Thanks for the code shortening tricks too!
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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