Home » excel » excel – Method 'Copy' of object '_Worksheet' failed

excel – Method 'Copy' of object '_Worksheet' failed

Posted by: admin April 23, 2020 Leave a comment

Questions:

For the first time I now faced the error

Method ‘Copy’ of object ‘_Worksheet’ failed

I heard that this is a quite common error but I was not able to find a solution. When I searched trough SO, I found many posts about the

‘Range’ of object failed-Error.

Is this error caused by memory limitations of the workstation or is it a software related error?

Anyone got an idea to work around that problem?

In case it is of interest that is my VBA code:

Private Sub CommandButton1_Click()
MsgBox "Message"

  Dim wb As Workbook
  Set wb = Workbooks.Add(xlWBATWorksheet)
  wb.Sheets(1).Name = "deletethissheet"

  Dim ws As Worksheet
  For Each ws In ThisWorkbook.Worksheets
    ws.Copy After:=wb.Sheets(wb.Sheets.Count)
  Next

  For Each ws In wb.Worksheets
    ws.UsedRange.Formula = ws.UsedRange.Value

    Dim sh As Shape
    For Each sh In ws.Shapes
      sh.Delete
    Next

  Next

  For Each Link In wb.LinkSources(xlLinkTypeExcelLinks)
  wb.BreakLink Name:=Link, Type:=xlLinkTypeExcelLinks
  Next

  wb.Sheets("deletethissheet").Delete
  wb.SaveAs Replace(ThisWorkbook.FullName, ".xlsm", "_" & Format(Date, "yyyymmdd") & ".xlsx"), xlOpenXMLWorkbook
  wb.Close SaveChanges:=False

End Sub

ws.Copy After:=wb.Sheets(wb.Sheets.Count)
Is the cause of the error.

How to&Answers:

Despite intensive research, I could not find any details about this error. The solution, which turned out to be very easy for me, was to manually copy everything onto a new sheet and start the macro from there. This error may be caused by limited resources or bad cells.

The VBA itself works. If anyone runs into this kind of problem rebuilding the sheet may work.