cowboysnut
New Member
- Joined
- Sep 22, 2015
- Messages
- 14
I have a script I have been using for years, but at one point it started crashing on me, but only sometimes. Usually it is when I open it fresh. If I run a few of the other macros in the workbook to sort of "prime the pump," it works just fine. I am using Excel 2016 for Mac. I'm trying to copy a sheet named "new," and paste it after another sheet already in the document (it's putting a sheet for a new individual in alphabetical order). I'm giving a little context in this script, but when it crashes, it crashes on the last line I am showing here. I cannot figure out why my code works sometimes but not others so I hope someone here can.
Error message: Run time error '1004': We couldn't copy this sheet.
Error message: Run time error '1004': We couldn't copy this sheet.
VBA Code:
Dim AfterSheet As String
Dim AfterFirst As String
Dim AfterLast As String
AfterFirst = ActiveCell.Offset(-1, 0).FormulaR1C1
AfterLast = ActiveCell.Offset(-1, 1).FormulaR1C1
AfterSheet = AfterFirst + "_" + AfterLast
Sheets("new").Copy After:=Sheets(AfterSheet)