Newbie VBA- Close Variable Defined Workbook

ackalbo

New Member
Joined
Mar 10, 2013
Messages
46
Hello all

I started to learn VBA code on Excel 2010 10 days ago and have "successfully" written the following basic code:

Sub OpenWorkbook()


'Opens Workbook & Copy/Paste Data

Dim FilePath As String
Dim FName As String

FilePath = "C:\Users\Kalbo\Desktop\Charonja Lollipop\LPOP\CashierSS\"
FName = FilePath & "CashierSS " & Format(Date - 1, "MM-DD-YYYY") & ".xlsm"

Workbooks.Open FName
Sheets("Data Entry").Select
Range("A3:F1003").Select
Selection.Copy

Workbooks("LZ Daily Sales Breakdown.xlsm").Activate
Sheets("Data Entry").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheets("Daily Overview").Select
Range("B3:E126").Select
Selection.Copy

Sheets("Monthly Overview").Select

Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheets("Daily Overview").Select

Range("H3:K126").Select
Selection.Copy

Sheets("Monthly Overview").Select

Range("B132").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheets("Data Entry").Select

Range("A3:F1003").Select
Selection.ClearContents


Workbooks(FName).Close False

This works great for what I need it to do except for the final line :( For the life of me I cannot seem to be able to close the variable defined workbook....I always get the error msg "Run-time error '9':Subscript out of range". I have tried everything that I can find on the internet but to no avail. I know that I am almost certainly missing something obvious (even consulted VBA Programming For Dummies) but as of now I cannot seem to get the last line to work.

Any assistance would be greatly appreciated. Many thanks guys & gals.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello all

I started to learn VBA code on Excel 2010 10 days ago and have "successfully" written the following basic code:



This works great for what I need it to do except for the final line :( For the life of me I cannot seem to be able to close the variable defined workbook....I always get the error msg "Run-time error '9':Subscript out of range". I have tried everything that I can find on the internet but to no avail. I know that I am almost certainly missing something obvious (even consulted VBA Programming For Dummies) but as of now I cannot seem to get the last line to work.

Any assistance would be greatly appreciated. Many thanks guys & gals.
At a guess your workbook name is wrong or the book is already closed
 
Upvote 0
Try this.
Code:
Sub OpenWorkbook()
Dim wbOpen As Workbook

Dim FilePath As String
Dim FName As String

   'Opens Workbook & Copy/Paste Data

   FilePath = "C:\Users\Kalbo\Desktop\Charonja Lollipop\LPOP\CashierSS\"
   FName = FilePath & "CashierSS " & Format(Date - 1, "MM-DD-YYYY") & ".xlsm"

   Set wbOpen =  Workbooks.Open(FName)

    ' other code 

   wbOpen.Close SaveChanges:=False
 
Upvote 0
Wow thank you Norie, worked like a charm. Looking at it, it's so simple but I would have never thought to do that. Thank you too galt13.

It's an uphill struggle but worth it hehe.

Many thanks.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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