c# - delete row based on a condition -
i coding in c# , using microsoft.office.interop.excel. have columns a-f , rows don't have value in column b. want loop through column b , find cells no text/value , delete entire row. manged loop through column b , find null cell, when try delete row- nothing happens. here code:
excel.range b = objsheet.get_range("b1:b" + lastusedrow, system.type.missing); foreach (excel.range r in b) { string column = r.text.tostring(); if (string.isnullorempty(column)) { excel.range bentirerow = objsheet.get_range(r + "1:" + r + "b" + lastusedcolumn, system.type.missing); // excel.range bentirerow2 = r.entirerow; bentirerow.delete(microsoft.office.interop.excel.xldirection.xlup); } }
rather looping through column can following; vba code:
range("b1:b" + lastusedrow).specialcells(xlcelltypeblanks).entirerow.delete
in c# use:
.specialcells(excel.xlcelltype.xlcelltypeblanks)
btw wouldn't name excel.application object excel
- use xl or myxl. confusing otherwise, , may cause errors @ point.
added full c# statement be
range("b1:b" + lastusedrow).specialcells(excel.xlcelltype.xlcelltypeblanks).entirerow.delete();
Comments
Post a Comment