What can cause Range.Copy error?
I have the following line in VBA code:
wks.Rows(lLastRow).Copy
It usually works fine. lLastRow is always 11. wks is always the active sheet.
But sometimes the line above starts throwing errors like "range class copy method failed".
Then I need to reload the Excel file for the line to start working again.
Do you have any idea what might be causing the problem? What can cause the operator to Range.Copy
stop working and then start working again after restarting the file?
EDIT : Here is my code. The code is triggered by a button callingNewActionButton()
Sub NewActionButton()
'Dim runState As New cRunState
'Set runState = New cRunState
If bProduction = True Then On Error GoTo ErrHandler
'START
Dim lStart As Long
Dim lId As Long
Dim lDate As Long
Dim lEnd As Long
Dim wks As Worksheet
Set wks = GetSheetByCodename(ThisWorkbook, "wActions")
lStart = 2
lId = 3
lDate = 2
lEnd = 12
Call NewRow(wks, lStart, lId, lDate, lEnd)
'END
On Error GoTo 0
Exit Sub
ErrHandler:
MsgBox ("Ett of" & oe & "rutsett fel har skett.")
End Sub
The procedure in which I am getting the error. the first time it fails, it seems to throw an error in the .Insert statement. Then if I run the macro again it throws an error in the .Copy expression. If I restart the document it works.
Sub NewRow(wks As Worksheet, lStartCol As Long, lIdCol As Long, lDate As Long, lEndCol As Long)
Dim lLastRow As Long
Dim rConstants As Range
Dim rConstant As Range
Dim rEarlierIds As Range
'Determine last row based on criteria column
lLastRow = LastRow(wks.Columns(lIdCol))
'Copy template row from directly above
wks.Range(wks.Cells(lLastRow, lStartCol), wks.Cells(lLastRow, lEndCol)).Copy
'Insert new row
wks.Range(wks.Cells(lLastRow + 1, lStartCol), wks.Cells(lLastRow + 1, lEndCol)).Insert
'Remove unwanted properties
Set rConstants = wks.Range(wks.Cells(lLastRow + 1, lStartCol), wks.Cells(lLastRow + 1, lEndCol)).SpecialCells(xlCellTypeConstants)
If Not rConstants Is Nothing Then
For Each rConstant In rConstants.Cells
rConstant = ""
Next rConstant
End If
'Add ID
Set rEarlierIds = wks.Columns(lIdCol)
wks.Cells(lLastRow + 1, lIdCol) = Evaluate("Max(" & rEarlierIds.Address & ")+1")
'Add Date
wks.Cells(lLastRow + 1, lDate) = Date
'Remove copy marking
Application.CutCopyMode = False
End Sub
EDIT2 : The .Insert statement inserts a new range. However, in spite of this, execution stops and I get the error "Insert method of the Range class could not be inserted. So the line does the job, but I get an error and the execution stops."
EDIT3 : I don't know what is wrong, but I found a pattern. The .Insert statement works if I run the macro immediately after opening the Excel file. And I can do it as many times as I want. But as soon as I double click on a cell and then press ESC to leave the cell and then try running the macro again, then the .Insert statement throws an error. It doesn't make sense to me.
EDIT4 . I have updated the code above as it currently looks like.
EDIT5 : The code below works flawlessly. What I changed is that I do not copy and paste. Instead, I first insert a new line and then copy it. I still don't know why my previous code didn't work. I hope someone has an idea or can confirm that I have stumbled upon an error and can say something about what circumstances are causing the error.
Sub NewRow(wks As Worksheet, lStartCol As Long, lIdCol As Long, lDate As Long, lEndCol As Long)
Dim lLastRow As Long
Dim rConstants As Range
Dim rConstant As Range
Dim rEarlierIds As Range
'Determine last row based on criteria column
lLastRow = LastRow(wks.Columns(lIdCol))
'Insert new row
wks.Range(wks.Cells(lLastRow + 1, lStartCol), wks.Cells(lLastRow + 1, lEndCol)).Insert
'Copy template row from directly above
wks.Range(wks.Cells(lLastRow, lStartCol), wks.Cells(lLastRow, lEndCol)).Copy
'Paste formulas row
wks.Range(wks.Cells(lLastRow + 1, lStartCol), wks.Cells(lLastRow + 1, lEndCol)).PasteSpecial xlPasteFormulas
'Remove values
Set rConstants = wks.Range(wks.Cells(lLastRow + 1, lStartCol), wks.Cells(lLastRow + 1, lEndCol)).SpecialCells(xlCellTypeConstants)
If Not rConstants Is Nothing Then
For Each rConstant In rConstants.Cells
rConstant = ""
Next rConstant
End If
'Add ID
Set rEarlierIds = wks.Columns(lIdCol)
wks.Cells(lLastRow + 1, lIdCol) = Evaluate("Max(" & rEarlierIds.Address & ")+1")
'Add Date
wks.Cells(lLastRow + 1, lDate) = Date
'Remove copy marking
Application.CutCopyMode = False
End Sub
source to share
This is probably because VB commands usually correspond to commands that Excel executes in its own interface. Most of the time this doesn't cause any problems - in fact, that's why macro writing works (even if it doesn't work very well). However, sometimes you get quirks. It smells like one of them (one of the others related to copy and paste is often any OTHER workbook level interactions than the paste command will throw an InvalidOperation exception when paste is called over time).
In this case, I would suggest copying some cells and then right clicking on any column or row. You will notice that the paste option is completely excluded - there is only the "Paste copied cells" option. This remains in effect as long as the worksheet is in CutCopyMode
; when it exits it, the Insert option is returned. My guess is either
- Your code is trying to paste (and not paste copied cells) when it is forbidden, or
- Your code tries to paste copied cells when the range it is trying to be pasted into is only ready to paste a few blank ones.
To be sure of this, you probably have to decompile both methods, which is beyond my scope. I'm glad to hear you've come up with a solution.
source to share