excel vba - How to refer correctly to a workbook from a cell that contains a volatile function? -


i have pretty stupid problem excel vba. i'm trying achieve function returns workbook property of workbook contains cell function inserted. ran problem whilst making function volatile , placing in personal workbook used of workbooks open (also addin). function -

function zsetservermetadata(byval metatypename string, optional byval newvalue string = "") string 'recalculate upon every time cell changes application.volatile  dim wb workbook set wb = application.activeworkbook  on error goto nosuchproperty  'if value defined on newvalue, set value , showoutput if newvalue <> ""     wb.contenttypeproperties(metatypename).value = newvalue     zsetservermetadata = wb.contenttypeproperties(metatypename).value     set wb = nothing     exit function 'if no value defined on newvalue show output leave content type unchanged else     zsetservermetadata = wb.contenttypeproperties(metatypename).value     set wb = nothing     exit function end if  nosuchproperty: zsetservermetadata = cverr(xlerrvalue) set wb = nothing end function 

now problem lies in line set wb = application.activeworkbook, , problem if have 2 or more workbooks open , activate other one, volatile function calculated value of "new" active workbook. not of course way want behave. need bit on make explicitly refer workbook function calculated , not calculate based on on activity of workbook (the workbook cell has function). ideas?

edit , solution gary's student

function zsetservermetadata(byval metatypename string, optional byval newvalue string = "") string 'recalculate upon every time cell changes application.volatile  'set wb pointer trough caller parents dim wb workbook, r range, ws worksheet set r = application.caller set ws = r.parent set wb = ws.parent  'clear unused elements set r = nothing set ws = nothing  on error goto nosuchproperty  'if value defined on newvalue, set value , showoutput if newvalue <> ""     wb.contenttypeproperties(metatypename).value = newvalue zsetservermetadata = wb.contenttypeproperties(metatypename).value set wb = nothing     exit function 'if no value defined on newvalue show output leave content type unchanged else     zsetservermetadata = wb.contenttypeproperties(metatypename).value     set wb = nothing     exit function end if  nosuchproperty: zsetservermetadata = cverr(xlerrvalue) set wb = nothing end function 

perhaps need ask caller:

public function lin()     dim r range, ws worksheet, wb workbook     set r = application.caller     set ws = r.parent     set wb = ws.parent     lin = wb.name end function 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -