Trying to close Excel file nut get error msg

  • Thread starter Thread starter LetMeDoIt
  • Start date Start date
L

LetMeDoIt

Greetings,

I'm created a new Excel file on the fly and copying a specific
worksheet into that new file. This part is fine, until I try to close
that new file (see last file of code) and get the error msg:

Run time error 9
subscript out of range.

Here's the logic in the code:


newFname = "NewEXCELfile.XLS"
Set NewWks = Workbooks.Add(1).Worksheets(1)
With NewWks
.Parent.SaveAs FileName:=newFname, FileFormat:=xlWorkbookNormal
End With
NewWks.Name = "MyNewSheet"
Set destrange = NewWks.Range("A1:z100")
Set sourceRange = ThisWorkbook.Sheets("SHEET1").Range("A1:z100")
sourceRange.Copy
destrange.PasteSpecial xlPasteColumnWidths, , False, False
destrange.PasteSpecial xlPasteFormats, , False, False
destrange.PasteSpecial (xlPasteFormulas)
application.CutCopyMode = False
Workbooks(newFname).Close

Many thanks...
 
Greetings,

I'm created a new Excel file on the fly and copying a specific
worksheet into that new file. This part is fine, until I try to close
that new file (see last file of code) and get the error msg:

Run time error 9
subscript out of range.

Here's the logic in the code:

newFname = "NewEXCELfile.XLS"
Set NewWks = Workbooks.Add(1).Worksheets(1)
With NewWks
.Parent.SaveAs FileName:=newFname, FileFormat:=xlWorkbookNormal
End With
NewWks.Name = "MyNewSheet"
Set destrange = NewWks.Range("A1:z100")
Set sourceRange = ThisWorkbook.Sheets("SHEET1").Range("A1:z100")
sourceRange.Copy
destrange.PasteSpecial xlPasteColumnWidths, , False, False
destrange.PasteSpecial xlPasteFormats, , False, False
destrange.PasteSpecial (xlPasteFormulas)
application.CutCopyMode = False
Workbooks(newFname).Close

Many thanks...

I was able to run your code without any errors... Is all of your code
in the list you provided? I'm using xl2007, what version are you
using?
 
Thanks for looking into it. I'm using Office 2003 running XP on a
laptop. Maybe I should try this on windows 2000.
 
I am using XP Office 2003 and it ran OK on mine.

LetMeDoIt said:
Thanks for looking into it. I'm using Office 2003 running XP on a
laptop. Maybe I should try this on windows 2000.
 
I rewrote part of the code to handle the closing. Many thanks for
testing this.
regards.
 

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

Back
Top