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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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,190,579
Messages
5,981,761
Members
439,733
Latest member
hmopheim

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
Top