excel vba - Function to get Usedrange gets error 91 -
trying write union of ranges function
i `object variable or block not set"
i not getting right (i think):
rng unionrange = intersect(ws.usedrange, rng.entirecolumn) end sub iunionrange() dim r range 'check see if function working set r = unionrange("elements", range("a1:d1, g1:g1, i1:k1")) r.select end sub
the function
function unionrange(shtname string, rng range) range set ws = thisworkbook.sheets(shtname) if rng nothing exit function ws.rng unionrange = intersect(ws.usedrange, .entirecolumn) end end function
first of all, use set
keyword assign object variable, unionrange =
should set unionrange =
. specify sheet object when retrieving range, doing it's not necessary pass sheet name function since rng.parent
returns sheet object.
there example below:
sub test() dim q range dim r range set q = sheets("elements").range("a1:d1, g1:g1, i1:k1") q.select set r = unionrange(q) r.select end sub function unionrange(rng range) range if rng nothing exit function set unionrange = intersect(rng.parent.usedrange, rng.entirecolumn) end function
Comments
Post a Comment