I use the VBA code below to copy the row above a button in the worksheet and insert that copy when directly above the row the button is on. Its purpose is to insert a new row for the user to add step information to a project management document.
For some reason, when I add information in one of the rows above this copied row, I get an error on the indicated line. The error is a
run-time error '1004: insert method of range class failed.
If I end and push the button again, I get a
run-time error '1004': Unable to get the TopLeftCell property of Button Class.
If I shut down Excel and reopen it, the button works fine. I usually add all the lines I need, enter the data, but when I go to add a new row, it get this series of errors again.
Is there a reason for this or are there better codes practices that will help me avoid this issue?
Also, I just noticed that it appears that another worksheet must be open for these error pattern to occur.
Sub rowselect()
Dim b As Object, cs As Integer
Dim Row2 As Integer
Dim Row As Integer
Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell ' 2nd error when code attempted again. Excel must be shut down for code to run properly
Row = .Row - 1
Row2 = Cells(Row, "e")
End With
Rows(Row).Select
Selection.Copy
Selection.Insert shift:=xlDown 'First error thrown here
Cells(Row + 1, "e") = Row2 + 1
Selection.EntireRow.Hidden = False
End Sub