excel - My code is stopping when I dont have blank cells in my workbook -


i creating vba code automates removal of merged cells , deletes blank rows created result of demurging. problem comes when tab doesnt have blank values. when sheet doesn't have blank values error 9. below code detect , delete blank rows documents:

range("a:a").specialcells(xlcelltypeblanks).entirerow.delete 

should try include if statement counteract this? in advance!

there couple ways go handling potential error of trying delete doesn't exist.

first, check see if there blank cells.

with worksheets("sheet1")     .range(.cells(1, 1), .cells(rows.count, 1).end(xlup))         if cbool(application.countblank(.columns(1)))             .cells.specialcells(xlcelltypeblanks).entirerow.delete         end if     end end  'earlier version of excel may not have countblank worksheets("sheet1")     .range(.cells(1, 1), .cells(rows.count, 1).end(xlup))         if application.counta(.columns(1)) < .rows.count             .cells.specialcells(xlcelltypeblanks).entirerow.delete         end if     end end 

the above has disadvantage countblank function count zero-length strings returned formula blanks while not considered trly blank .specialcells(xlcelltypeblanks) method. however, wouldn't looking blanks in column know populated formulas consideration, not deal breaker.

next can test nothing altering error handling method.

dim delrng range worksheets("sheet1")     .range(.cells(1, 1), .cells(rows.count, 1).end(xlup))         'temporarily suspend error handling         on error resume next         set delrng = .cells.specialcells(xlcelltypeblanks)         on error goto 0         if not delrng  nothing             delrng.entirerow.delete         end if     end end 

although accepted, not favor method because don't think should have break in order see if exists personal preference.


Comments