excel - Deleting Rows From Filtered Range Via Macro -


i having issues below code, trying filter set of data, delete visible rows. "run time 1004 error: delete method or range class failed" occurs on last line of code. found similar question on site, , answer appears have in last line of code.

dim lastrow long  lastrow = worksheets("orders").range("a" & rows.count).end(xlup).row  worksheets("orders").range("a1:cu" & lastrow).autofilter field:=10,  criteria1:="new"  if worksheets("orders").range("a1:cu" & lastrow).specialcells(xlcelltypevisible).count > 1  worksheets("orders").range("a1:cu" & lastrow).offset(1, 0).specialcells _ (xlcelltypevisible).entirerow.delete 

later in code, filter same set of data again, time deleting non-visible (filtered out) rows. getting same error on last line of code well:

   worksheets("orders").range("$a1:cc" & lastrow).autofilter field:=26, criteria1:= _     "=*jazz*", operator:=xland   dim orow range, rng range dim myrows range sheets("orders")     set myrows = intersect(.range("a:a").entirerow, .usedrange)     if myrows nothing exit sub end  each orow in myrows.columns(1).cells     if orow.entirerow.hidden         if rng nothing             set rng = orow         else             set rng = union(rng, orow)         end if     end if next  if not rng nothing rng.entirerow.delete 

any on these errors appreciated!

you dont need invoke specialcells before deleting, because method delete acts on visible rows. problem might due this.

besides, test .specialcells(xlcelltypevisible).count > 1 useless because header row of filtered range remains visible.

here's safer , simpler way write code:

with worksheets("orders").usedrange   .autofilter 10, "new"   .offset(1).entirerow.delete        '     <--- no need .specialcells end 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -