excel - SpecialCells(xlCellTypeConstants) results unexpected result on just one cell as the base range -
to summarize: if base range of specialcells 1 cell, result of range.specialcells(xlcelltypeconstants, xlnumbers) yields not cell or nothing. supposed, specialcells should yield range inside object applied... or wrong?
i've "developed" little new excel book, show don't understand, follows:
created new excel book
filled a1:b3, follows:
b ... 1 1 12 2 2 22 3 3 32 .
all other cells of sheet untouched.
inside new module i've added following code:
private sub test() dim oset range, ospec range, oused range worksheets("sheet1").activate set oset = activesheet.range("a1:a1") set ospec = oset.specialcells(xlcelltypeconstants, xlnumbers) set oused = activesheet.usedrange set oset = nothing set ospec = nothing set oused = nothing end sub
running subroutine, , stopping @ first range reset, yields:
oset.address = "$a$1" ospec.address = "$a$1:$b$3" ' seems wrong; should "$a$1" ? oused.address = "$a$1:$b$3"
having been changed value of a1 a, rerun sub, , stop @ same place, gives (consistently previous, shows works consequtively):
oset.address = "$a$1" ospec.address = "$b$1","$a$2:$b$3" ' seems wrong; should ospec nothing ? oused.address = "$a$1:$b$3"
however, resetting value of a1 original 1, changing range of first set operation in subroutine cell "a1" true range of cells "a1:a2", rerun sub, , stop @ place, gives different (and more expected) result:
oset.address = "$a$1:$a$2" ospec.address = "$a$1:$a$2" ' oused.address = "$a$1:$b$3"
i appreciate, if explain results. ahead.
this happening because in case of 1 cell, considers usedrange instead.
references:
1) super secret specialcells
2) using specialcells in excel vba
as helpful note, give wrapper function specialcells , helper method (in c#, not in vba):
/// <summary> /// <para>wrapper specialcells function.</para> /// </summary> /// <param name="inputrange"></param> /// <param name="celltype"></param> /// <returns></returns> /// <remarks>throws null when there no cells in <paramref name="inputrange"/> corresponding <paramref name="celltype"/>, unlike specialcells throws exception</remarks> public static range getrangespecialcells(this microsoft.office.interop.excel.range inputrange, xlcelltype celltype) { try { if (inputrange.cells.count == 1) { if (celltype == xlcelltype.xlcelltypecomments) { if (inputrange.comment != null) { return inputrange; } else { return null; } } else if (celltype == xlcelltype.xlcelltypeformulas) { if (inputrange.hasformula == true) { return inputrange; } else { return null; } } else if (celltype == xlcelltype.xlcelltypeblanks) { if (string.isnullorempty(inputrange.value2) == true) { return inputrange; } else { return null; } } else if (celltype == xlcelltype.xlcelltypelastcell) { return inputrange; } else { // since inputrange has single cell, specialcells apply entire worksheet // range has cells worksheet (usedrange) of type provided: celltype range temp = inputrange.specialcells(celltype); // intersect range single cell (inputrange) above range range rangeoverlap = intersect(inputrange, temp); // if range single cell contained in intersection, cell of type xlcelltypeconstants if (rangeoverlap.count == inputrange.count && rangeoverlap.rows.count == inputrange.rows.count && rangeoverlap.columns.count == inputrange.columns.count) { return inputrange; } else { return null; } } } else { return inputrange.specialcells(celltype); } } catch (system.runtime.interopservices.comexception ex) { return null; } } /// <summary> /// <para>customized function intersection of 2 ranges (<paramref name="rangea"/> ∩ <paramref name="rangeb"/>)</para> /// </summary> /// <param name="rangea"></param> /// <param name="rangeb"></param> /// <returns>range corresponding intersection of 2 provided ranges</returns> /// <remarks>this function returns null if of provided ranges null or malformed, unlike application.intersect throws exception</remarks> public static range intersect(range rangea, range rangeb) { range rngintersect; if (rangea == null) { rngintersect = null; } else if (rangeb == null) { rngintersect = null; } else if (rangea.worksheet != rangeb.worksheet) { rngintersect = null; } else { try { rngintersect = globals.thisaddin.application.intersect(rangea, rangeb); } catch (exception ex) { rngintersect = null; } } return rngintersect; }