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

Subscript out of range error on VBA code in Excel

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I keep getting a Subscript out of range error on this part of code. I know it works as I've used the code in other things and recording a macro gives almost the exact code.

Code:
OldWorkbookName = ActiveWorkbook.Name
Workbooks.Add
NewWorkbookName = ActiveWorkbook.Name
    
Windows(OldWorkbookName).Activate
Selection.Cut     'Selection is made else where in the code
Windows(NewWorkbookName).Activate 'This line is the error
Range("A1").Select
ActiveSheet.Paste

Any idea what might be happening?
 
Hmmmmm. Works for me.

Did you save the new workbook somewhere in the middle of that? That would make the value in NewWorkbookName obsolete.
 
It does shortly after that code, but not during or before. That is how the code is from start to finish for the copy. All it has to do is copy the info it has from the old workbook to the new, print the new, save the new, and close everything.
 
If found it. It is some combination of if you put it in the Workbook_Open event, Active one workbook, Cut (or copy), and then try to call another workbook for it to not work. Any of those on their own worked fine for me, but you do all of it and it breaks. To fix it I just had to move it to a separate module and have the Workbook_Open even call the function there for it to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top