excel vba - Delete Rows & Maintain Input Range -
excel vba - Delete Rows & Maintain Input Range -
i wonder whether may able help me please.
for few weeks i've been trying find solution whereby users can following:
delete rows , without data, shift rows containing info aso sit down 1 under another, whilst maintaining defined 'input range'i've set next script clears cell contents , hence doesn't alter 'input range'.
sub delrow() dim msg sheets("input").protect "handsoff", userinterfaceonly:=true application.enablecancelkey = xldisabled application.enableevents = false msg = msgbox("are sure want delete row?", vbyesno) if msg = vbno exit sub selection application.intersect(.parent.range("a:s"), .entirerow).interior.colorindex = xlnone application.intersect(.parent.range("t:ae"), .entirerow).interior.colorindex = 42 selection.specialcells(xlcelltypeconstants).clearcontents application.intersect(.parent.range("c:ae"), .entirerow).locked = true application.intersect(.parent.range("ag:ag"), .entirerow).locked = true end application.enableevents = true end sub
updated code
sub delrow() dim rangetoclear range dim msg vbmsgboxresult 'sheets("input").protect "handsoff", userinterfaceonly:=true application.enablecancelkey = xldisabled application.enableevents = false msg = msgbox("are sure want delete row?", vbyesno) if msg = vbno exit sub selection application.intersect(.parent.range("a:s"), .entirerow).interior.colorindex = xlnone application.intersect(.parent.range("t:ae"), .entirerow).interior.colorindex = 42 on error resume next set rangetoclear = selection.specialcells(xlcelltypeconstants) on error goto 0 ' or defined error handler if not rangetoclear nil rangetoclear.clearcontents else selection.sort key1:=range("b7"), order1:=xlascending, header:=xlno, _ ordercustom:=1, matchcase:=false, orientation:=xltoptobottom, _ dataoption1:=xlsortnormal end if application.intersect(.parent.range("c:ae"), .entirerow).locked = true application.intersect(.parent.range("ag:ag"), .entirerow).locked = true end application.enableevents = true end sub
the problem though, if user selects blank row receive 'error 400' message , doesn't shift rows sit down underneath each other.
as said, i've spent much time on trying find solution without success.
i grateful if @ please , offer guidance on how may accomplish this.
many , kind regards
if selection blank, line selection.specialcells(xlcelltypeconstants).clearcontents
fail because there no xlcelltypeconstants
. need test , clear content if there any:
edit: seek reply sorting question
i think want sort no matter what, moved sort
after clearcontents
. sorted usedrange though, don't think want. need define range sorted, either named range using name manager in excel, or in code.
sub delrow() dim rangetoclear range dim msg vbmsgboxresult sheets("input").protect "handsoff", userinterfaceonly:=true application.enablecancelkey = xldisabled application.enableevents = false msg = msgbox("are sure want delete row?", vbyesno) if msg = vbno exit sub selection application.intersect(.parent.range("a:s"), .entirerow).interior.colorindex = xlnone application.intersect(.parent.range("t:ae"), .entirerow).interior.colorindex = 42 on error resume next set rangetoclear = selection.specialcells(xlcelltypeconstants) on error goto 0 ' or defined error handler if not rangetoclear nil rangetoclear.clearcontents end if 'you need define range want sorted 'here i've used usedrange activesheet.usedrange.sort key1:=range("b7"), order1:=xlascending, header:=xlno, _ ordercustom:=1, matchcase:=false, orientation:=xltoptobottom, _ dataoption1:=xlsortnormal application.intersect(.parent.range("c:ae"), .entirerow).locked = true application.intersect(.parent.range("ag:ag"), .entirerow).locked = true end application.enableevents = true end sub
excel-vba excel-2003
Comments
Post a Comment