Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to close a file in excel (vba)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do I close a workbook in excel. When I use the following code:

workbooks(workbookname).close

an error appears : (error 1004: method failed).

When only one workbook is open (and none are hidden) then this method does work.

 
You'd be much better served to put this question in the vba forum.
penny.gif
penny.gif
 
Hello, Bobby V.

Make sure your workbook(workbookname) is activate before closing it. Also make sure workbookname is enclosed by quotation marks.

Check the following little script on you machine, see if it works as expected.

'---------------------------
Option Explicit
Dim objXL
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Dim wbs
Set wbs = objXL.Workbooks

wbs.Add()
wbs.Add()
wbs.Add()
wbs.Add()
WScript.Echo "Now you have four workbooks."

wbs("book2").Activate
WScript.Echo "You have activate book2."
wbs("book2").Close
WScript.Echo "You have closed book2."
WScript.Echo "The focus returns to last-but-one focus ie book4." & _
"Verify this fact before clicking OK."

wbs.Close
Set wbs = Nothing
objXL.Quit
Set objXL = Nothing
WScript.Quit
'----------------------------

Try take away Activate line, it will close the wrong workbooks (book4).
Try other possibility by modifying it, see if it can simulate your runtime error.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top