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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a file in Excel

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
When I use the following code, an error appears (runtime-error 1004: Method of class failed)

Sub insertData()
dim workbookname as string

DlgAnswer = Application.Dialogs(xlDialogOpen).Show
workbookname=activeworkbook.name
...

workbooks(workbookname).close

end sub

The purpose is to open a second workbook, retrieve data from it, and then close this workbook.
However, this code only works if only 1 sheet is open. From the moment that 2 sheets are open (even if one sheet is hidden), the code produces the error 1004.

What am I doing wrong?
 
Consider this:

ChDir "C:\Inventory Files"
Workbooks.Open FileName:="C:\InventoryFiles\REPORTsf.xls"
Cells.Select
Selection.Copy
Workbooks("PLZ1Plan.xls").Activate 'this is already open
Cells.Select
Application.DisplayAlerts = False
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks("REPORTsf.xls").Activate
Application.DisplayAlerts = False
ActiveWorkbook.Close
Workbooks("PLZ1Plan.xls").Activate



If I'm way off let me know:
drat@mediaone.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top