Debug error help: Method 'Copy' of Object '_Worksheet' failed

Gti182

Board Regular
Joined
Dec 7, 2011
Messages
65
Hi all,

I'm having an odd issue with my below macro.

The macro copies a tab from a current file and then does some manipulation then save's it to a specified folder.
(note: some non relevant code has been omitted to keep it short)

The code works great on first try but if i execute the macro for the second time i get a debug error on red portion of the code "Method 'Copy' of Object '_Worksheet' failed"

Any way i can prevent this from happening?

Rich (BB code):
Sub REPORTSG2()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
      
Dim wb1 As Workbook

SPath = "Z:\Template\" ' Save Path of report

Sheets("Sheet1").Copy

Set wb1 = ActiveWorkbook

ActiveSheet.Shapes("Button 1").Delete ' Delete macro buttons
ActiveSheet.Shapes("Button 2").Delete
        
ActiveWorkbook.SaveAs FileName:=SPath & "\" & "Test" & ".xlsx", FileFormat:=51 ' Define how to save file
wb1.Close
  
MsgBox "Process Complete! " & vbCrLf & "" & vbCrLf & "Files Saved in: " & vbCrLf & "" & vbCrLf & "" & SPath, vbInformation
' Message Box with path location of saved file

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
thats quite an interesting link but i think it refers to a loop which my code doesn't have? Code used does save it as a single sheet workbook also.

still scratching my head at this one...
 
Upvote 0
You might need to post the rest of the code you are using. Nothing in what you have posted suggests any kind of error should occur. It's not clear what you mean when you say you "run it a second time". Clearly it would work if you closed all your excel files, opened up the workbook, and run it again. So there is something in this "running a second time" that needs to be clarified.
 
Upvote 0
Full code below.

To clarify running a second time: The master file (file containing below code) remains open at all times.
Once the macro is run it creates Project2.xlsx file (modified/filtered version of the current tab in the master file) and saves it to a specific location on the network.

I can run below macro over and over without problem. The error only occurs when i run the macro (creates Project2.xlsx file), open and close this file, try rerun the macro.

To note: this problem doesn't occur if i save the Master file before rerunning the macro after opening/closing Project2.xlsx.

hope its a bit clearer now.

Code:
Sub Export2t()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
      
SPath = "W:\17.0 Projects\12.14_Projects" ' Save Path of report

Sheets("Projects").Copy

Dim wb1 As Workbook

Range("N2:AB363").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
    ActiveWindow.LargeScroll ToRight:=-1
    Application.GoTo Cells(1, 1)
    
Set wb1 = ActiveWorkbook

ActiveSheet.Shapes("Button 1").Delete
ActiveSheet.Shapes("Button 2").Delete
ActiveSheet.Shapes("Button 3").Delete
        
Range("N:O,Q:BI").Delete

Dim MonthsAhead
MonthsAhead = InputBox("Please enter the number of months ahead to be displayed:", , 3)
If Not IsNumeric(MonthsAhead) Then MonthsAhead = 3

Range("P1").formula = "=EDate(TODAY()," & MonthsAhead & ")"
Range("Q1").formula = "=TODAY()"
    
Range("P1:Q1").Select
Selection.NumberFormat = "General"
ActiveSheet.Range("P1:Q1").Font.Color = vbWhite
    
ActiveSheet.Range("A1:N500").AutoFilter Field:=14, Criteria1:= _
 ">" & Range("Q1").Value, Operator:=xlAnd, Criteria2:="<" & Range("P1").Value
 
ActiveWorkbook.Worksheets("Projects").AutoFilter.Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Projects").AutoFilter.Sort. _
SortFields.Add Key:=Range("N1:N500"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Projec").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
End With
 
Application.GoTo Cells(1, 1)

Rows("1:1").RowHeight = 55

ActiveWorkbook.SaveAs FileName:=SPath & "\" & "Export2" & ".xlsx", FileFormat:=51

wb1.Close
  
MsgBox "Process Complete! " & vbCrLf & "" & vbCrLf & "Files Saved in: " & vbCrLf & "" & vbCrLf & "" & SPath, vbInformation
' Message Box with path location of saved file

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True

End Sub
 
Last edited:
Upvote 0
Okay. Well, I don't see anything at all that would cause this problem. It may be related to that bug shg mentioned, after all, even though it's not in a loop. You could maybe work around by adding a new workbook and copying the data from the sheet to it, rather than copying the sheet itself.
 
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,538
Members
449,456
Latest member
SammMcCandless

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
Back
Top