Workbooks.Close and Open??

Chuck6475

Board Regular
Joined
Sep 30, 2012
Messages
126
2003 / w7

Things have been going swimmingly and then.....

I used the Workbooks.Open

Code:
    FileName = Application.GetOpenFilename(fileFilter:="Excel Files  (*.xls), *.xls")
    MsgBox "You selected " & FileName
    
    If FileName = False Then Exit Sub
    
    Workbooks.Open FileName:=FileName

All is well. It works great. After finishing doing my thing to this workbook/worksheet, I want to Close it without saving changes. Seemed to be a simple thing to do. I'd just use Workbooks.close

Well the results have been less than wonderful and confusing, at least to me.

My first attempt was: Workbooks("filename").close savechanges:=false
or: Workbooks(filename).close savechanges:=false

I figured I was in trouble when VB didn't capitalize savechanges and I was right.

Now I understand that FileName in my case if the full file name for the file, path included. Surely VBA can handle that? Well it appears the answer is NOT. If I spellout the filename without the path it works, but it is not acceptable.

namely: Workbooks("Feb 1 2013 handicaps").Close savechanges:=False
Still doesn't capitalize savechanges however, curious.

This line of code works, obviously if the file name is "Feb 1 2012 handicaps". There has to be a way to make this dynamic. Is there a magic VBA statement that removes the path from a filename for example.

Thanks for listening to this rant.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this.
Code:
FileName = Application.GetOpenFilename(fileFilter:="Excel Files  (*.xls), *.xls")
    MsgBox "You selected " & FileName
    
    If FileName = False Then Exit Sub
    
    Set wb = Workbooks.Open(FileName:=FileName)

    ' other code

    wb.Close SaveChanges:=False
 
Upvote 0
instead of:
workbooks.open filename:=FileName
use:
set blah = workbooks.open(fileName)

later use:
blah.close false

or...
use:
With workbooks.open(filename:=FileName)

'.your code
.close false
end with
 
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