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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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