excel - Run time error 1004 when deleting a selected row -
i have below code deletes 3 lines within designated worksheet (finding ranges etc). code have used atleast year without issues. have transferred code new work book , set worksheet same previous workbook.
the code errors highlighted below error message
run-time error '1004'
delete method of range class failed
can suggest why error occur?
sub deleterowpic() application.screenupdating = false application.calculation = xlmanual activesheet.unprotect password:="projects123" activesheet.range("total").select if selection.row = 12 else activesheet.range("total").select selection.offset(-2, 0).select activecell.entirerow.delete activesheet.range("total_1").select selection.offset(-2, 0).select activecell.entirerow.delete **error occurs here** activesheet.range("total_2").select selection.offset(-2, 0).select activecell.entirerow.delete end if range("k2").select application.calculation = xlautomatic activesheet .protect password:="projects123", userinterfaceonly:=true .enableoutlining = true end
as mentioned in comments, should start last row , work way first row when deleting rows. suspect issue caused this. without seeing data, suggest working total_2
total
. also, should avoid using select
whenever possible. try modifying code this:
sub deleterowpic() dim ws worksheet application.screenupdating = false application.calculation = xlmanual set ws = activesheet ws .unprotect password:="projects123" if .range("total").row <> 12 .range("total_2").offset(-2, 0).entirerow.delete .range("total_1").offset(-2, 0).entirerow.delete .range("total").offset(-2, 0).entirerow.delete end if application.calculation = xlautomatic .protect password:="projects123", userinterfaceonly:=true .enableoutlining = true end
make sure set application.screenupdating
true @ point well.
Comments
Post a Comment