Subscript out of range error copying worksheet to new workbook

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,812
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need some help ...

Rich (BB code):
Fpath = "U:\PWS\Parks\Parks Operations\Sports\WATSOP19\STAFF\BackupData\"
fname = "staff_schedule_BU (" & Format(Now, "dd-mm-yy hh,mm,ss") & ").xlsm"
Set newbook = Workbooks.Add
wb_pstaff.Worksheets("STAFF_SCHEDULE").Copy after:=newbook.Worksheets("Sheet1")
If Dir(Fpath & fname) <> "" Then
    MsgBox "File " & Fpath & fname & " already exists.", vbExclamation, "FILE ERROR"
Else
    newbook.SaveAs filename:=Fpath & fname
    Set wb_staffschedulebu = Workbooks(fname)
    MsgBox "Backup : " & fname & " secured.", vbInformation, "SUCCESS"
End If
wb_staffschedulebu.Close

I am receiving a "Subscript out of range" error with the line in red.

Worksheet "STAFF_SCHEDULE" is found in workbook alias wb_pstaff (which is recognized properly) and I wish to copy it to a newly created workbook (newbook) after "Sheet1"

Thoughts?
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,763
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
I can't test......but I think you need

Code:
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=Aname & ".xlsm"
Set newbook=activeworkbook
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,812
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thank you Michael, I'm not sure if your suggestion would be a solution. I'm not having any problem creating or saving the new workbook, just copying a worksheet from one book to the other.

Much appreciate the effort!
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,812
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
It must be getting late ... such a foolish error.
Its all good friends. Helps if I use the correct workbook.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,763
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
:LOL::LOL:....doesn't have to be late for me to make those !!!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,961
Messages
5,599,061
Members
414,281
Latest member
Engjamal2021

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top