Workbook won't close using VBA

stephen antoine

Board Regular
Joined
Jun 4, 2008
Messages
128
Hello all,

For some reason the code below gets an error saying the subscript is out of range once it gets to the last step...

Code:
CM_Current_Payments_Export = "J:\Utilities\Building Rent\FY2008\Rent Reconciliation\05 - Jun\Export_Current_Payments_Active_Jun-2008.xls"
 
Workbooks.Open Filename:= _
        CM_Current_Payments_Export
    Cells.Copy
    Windows(CM_PP_Rec).Activate
    Sheets("Current_Payments").Paste
Workbooks(CM_Current_Payments_Export).Close savechanges:=False

Does anyone know why the error is popping up when the sub tries to close the workbook?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Workbooks.Open - Requires Path and Filename
Workbooks(book).Close - DOES NOT use Path...
so you should separate your variables into 2
1st for path
2nd for filename

Example..

Code:
MyPath ="J:\Utilities\Building Rent\FY2008\Rent Reconciliation\05 - Jun\"

CM_Current_Payments_Export = "Export_Current_Payments_Active_Jun-2008.xls"
 
Workbooks.Open Filename:= _
        MyPath & CM_Current_Payments_Export
    Cells.Copy
    Windows(CM_PP_Rec).Activate
    Sheets("Current_Payments").Paste
Workbooks(CM_Current_Payments_Export).Close savechanges:=False

Hope that helps..
 
Upvote 0
Stephen

One way you could have avoided this would be to create a reference to the workbook when you open it.
Code:
Dim wbOpen As Workbook
    CM_Current_Payments_Export = "J:\Utilities\Building Rent\FY2008\Rent Reconciliation\05 - Jun\Export_Current_Payments_Active_Jun-2008.xls"
 
    Set wbOpen = Workbooks.Open(Filename:=CM_Current_Payments_Export)
    With wbOpen
        .ActiveSheet.Cells.Copy Workbooks(CM_PP_Rec).Sheets("Current_Payments").Range("A1")
        .Close.Close savechanges:=False
    End With
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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