Closing a workbook programmatically

R

Raul

I am loosely using the workbooks.open method to open a
workbook and then extracting some data from it. The
routine opens the workbook and gets the desired
information. However, I'd like to close the workbook
before moving on to other steps but I can't seem do it.

Can anyone help me out with this one?

......
Workbooks.Open "K:\AAAA\BBBB\CCCC\excelfile.xls"
Set currentBook = Application.ActiveWorkbook
'do other stuff

Workbooks(currentBook).Close
.....
The routine doesn't crash, it just doesn't close the
workbook. I don't need to save any changes to the file I
want to close. I also have other workbooks open that I
don't want closed.

Thanks,
Raul
 
R

Ron de Bruin

Hi Raul

Try this

Sub test()
Dim destWB As Workbook
Set destWB = Workbooks.Open("K:\AAAA\BBBB\CCCC\excelfile.xls")
MsgBox "By"
destWB.Close True ' false = not save and true = save
End Sub
 
R

Raul

Thanks a bunch. I found the error in my code. The code
wasn't getting the name of the file I wanted to close.
After I added
"WorkBookName = Application.ActiveWorkbook.Name"
and in the code and then used WorkBookName instead of
currentBook in the following statement it worked
"Workbooks(WorkBookName).Close SaveChanges:=False"

I'll try your suggestion also. I was dimensioning
currentBook As Excel.WorkBook

Is this correct?

Thanks again
 
J

Juan Pablo González

Yep, that's correct. You're working with the object (The workbook) directly
instead of accessing the collection.

--
Regards

Juan Pablo González

Thanks a bunch. I found the error in my code. The code
wasn't getting the name of the file I wanted to close.
After I added
"WorkBookName = Application.ActiveWorkbook.Name"
and in the code and then used WorkBookName instead of
currentBook in the following statement it worked
"Workbooks(WorkBookName).Close SaveChanges:=False"

I'll try your suggestion also. I was dimensioning
currentBook As Excel.WorkBook

Is this correct?

Thanks again
 
R

Raul

Thanks a bunch. I found the error in my code. The code
wasn't getting the name of the file I wanted to close.
After I added
"WorkBookName = Application.ActiveWorkbook.Name"
and in the code and then used WorkBookName instead of
currentBook in the following statement it worked
"Workbooks(WorkBookName).Close SaveChanges:=False"

Your code snippet answered another question I had about
the Dim statement. Does the Set destWB = Workbooks.Open
do more than a plain Workbooks.Open command?

Thanks,
Raul

-----Original Message-----
Hi Raul

Try this

Sub test()
Dim destWB As Workbook
Set destWB = Workbooks.Open ("K:\AAAA\BBBB\CCCC\excelfile.xls")
MsgBox "By"
destWB.Close True ' false = not save and true = save
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Raul" <[email protected]> wrote in
message news:[email protected]...
 
G

Guest

Thanks,
Raul
-----Original Message-----
Yep, that's correct. You're working with the object (The workbook) directly
instead of accessing the collection.

--
Regards

Juan Pablo González

Thanks a bunch. I found the error in my code. The code
wasn't getting the name of the file I wanted to close.
After I added
"WorkBookName = Application.ActiveWorkbook.Name"
and in the code and then used WorkBookName instead of
currentBook in the following statement it worked
"Workbooks(WorkBookName).Close SaveChanges:=False"

I'll try your suggestion also. I was dimensioning
currentBook As Excel.WorkBook

Is this correct?

Thanks again



.
 
R

Ron de Bruin

Hi Raul
Does the Set destWB = Workbooks.Open
do more than a plain Workbooks.Open command?

It will open and set a reference to the file
In your code you use two lines to do this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top