0

I'm using the below code to move a worksheet from one open workbook to another open workbook but it's breaking on the last line of the code. From everything that I see online, it looks like this code should work.

Error MSG : (Run-time error '9') (Subscription out of range)

Sheets("Summary ML").Select

Workbooks.Open Filename:=outputfile
Windows(wb).Activate
Sheets("Summary ML").Select
Sheets("Summary ML").Copy After:=Workbooks("outputfile.xlsx").Sheets(2)
ForRace
  • 96
  • 7
DZIQ
  • 1
  • 1
  • Does `Workbooks("outputfile.xlsx").Sheets(2)` exist? I am talking about i) the workbook itself and ii) the sheet with index 2 – Storax Sep 03 '19 at 17:48
  • Yes, the workbook is being opened prior to the code where i try to copy over the new sheet. The workbook has 1 sheet currently in it when it's opened and I'm trying to copy a new sheet after the sheet that's already there. – DZIQ Sep 03 '19 at 18:02
  • 1
    If thwe workbook has 1 sheet only then `Sheets(2)` will run into an error – Storax Sep 03 '19 at 18:13
  • see how I did it, not saying it's the best though: https://stackoverflow.com/a/30605765/4961700 – Solar Mike Sep 03 '19 at 18:24
  • @DZIQ , May I Ask you to leave me some feedbacks on the answer I proposed ? If it answers to your needs May I ask you to accept my answer : [look here to see how to accept](https://meta.stackexchange.com/a/5235) – TourEiffel Sep 11 '19 at 15:37

1 Answers1

0

Maybe you can use this :

Workbooks("Book1.xlsx").Worksheets.Copy Before:=Workbooks("Book2.xlsx").Sheets(1)

If you don't know the meaning of sheet(1) you can do debug.print sheet(1).Name

Kindest regards.

TourEiffel
  • 4,034
  • 2
  • 16
  • 45