Forum Discussion

GregSchedler's avatar
GregSchedler
Copper Contributor
Apr 15, 2020

Recorded Macro not working

I have a recorded Macro that is not working. The Run-time error '1004': error occurs at the xlToRight line.  The error says "Application-defined or object-defined error". The same is happening in a much larger Macro that worked last Friday. This past Monday I had to re-install Excel. In the larger macro, I also had to change all "Range ("C11") to ActiveSheets.Cells(11,3) because they errored. I am a relative newbie to VBA and can't figure out the problem even after hours of web searching. Thanks for your help!

 

Sub Macro7()
'
' Macro7 Macro
'

'
Sheets("Pivot 1").Select
ActiveCell.Offset(-1, 0).Range("A1").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
Range("C11").Select

End Sub

 

1 Reply

  • GregSchedler 

    Has this been solved? If not, let's assume that active cell is A2. 

    ActiveCell.Offset(-1, 0).Select

    This command moves active cell to A1. If you had started in cell A1 it would have caused an error because you can't move 1 row up from cell A1.

    Selection.End(xlToRight).Select
    This command moves the active cell to XFD1.

    ActiveCell.Offset(0, 1).Select

    This causes an error because column XFD is the last column of the sheet. Active cell therefore can't be moved to XFE1.

     

    ActiveCell.Offset(-1, 0).Range("A1").Select

    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select

     

    To my understanding the code colored in orange isn't required here.

Resources