Excel: Can custom units for be defined for the CONVERT function? -


in excel, can convert inches feet =convert(a1,"in","ft") (where a1 contains number converted), or bytes bits =convert(a1,"byte","bit). convert function works excel's predefined units.

is there way define custom units such "piece" or "dozen" or "tub"? i've been using excel's "bit" unit in place of "piece". i'd conversions such =convert(3,"dozen","piece") (the result of evaluation should 36).

of course, 1 maintain table of custom units conversions standard units (e.g. 1 piece = 1 bit, 1 dozen = 12 bits). conversion taken care of using modified convert function =myconvert(3,"dozen","piece") custom units table. there elegant way make original convert function work, avoiding workarounds adding excel's list of predefined units?

warning: doesn't answer exact question, seek provide potential solution.

i don't know of way precisely ask... maybe there compromise solution can use udf , have udf first call worksheet function. if works, return result. otherwise, go custom function. means have replace instances of convert myconvert, which, limited vantage point, seems reasonable trade-off.

here quick sample of how might look.

custom conversion boilerplate:

private function factor(unit string) double    select case lcase(unit)     case "dozen"       factor = 12     case "dz"       factor = 12     case "unit"       factor = 1     case "piece"       factor = 1     case "each"       factor = 1     case "ea"       factor = 1     case "gross"       factor = 144     case else       factor = 0   end select  end function 

and udf:

function myconvert(val double, fromunit string, tounit string) variant    on error goto custom   myconvert = worksheetfunction.convert(val, fromunit, tounit)   exit function  custom:   dim fromfactor, tofactor double   fromfactor = factor(fromunit)   tofactor = factor(tounit)    if fromfactor = 0 or tofactor = 0     myconvert = cverr(xlerrna)     exit function   end if    myconvert = val * (fromfactor / tofactor)  end function 

and of these should work:

=myconvert(a1,"in","ft") =myconvert(3,"dozen","piece") 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -