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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -