vba - Parsing the Parameters of a Function -
i trying create udf within vba go through function syntax , treat text.
the function :
functiona( param1 , param2 , param3 , param 4 )
i trying develop udf pull out value of param based on position input udf function.
getn( functiona , 3 ) = "param3" getn functiona , 1 ) = "param1"
here's function far it's off....
it's behaving :
getn( functiona , 0 ) = param2
here's function:
function getn(sinputstring string, n integer) string dim sfindwhat string dim j, finda, findb integer application.volatile sfindwhat = "," finda = 0 j = 0 n finda = instr(finda + 1, sinputstring, sfindwhat) findb = instr(finda + 1, sinputstring, sfindwhat) if findb = 0 findb = instr(finda + 1, sinputstring, ")") if finda = 0 exit next getn = trim(mid(sinputstring, finda + 1, findb - finda - 1)) end function
thank help
split
should work, though correctly handle case of nested functions, preliminary hack first replace commas @ top level safe delimiter (e.g. [[,]]
) , splitting on delimiter:
function getparametern(func string, n long) string dim args variant dim safeargs string dim c string dim long, pdepth long func = trim(func) = instr(func, "(") args = mid(func, + 1) args = mid(args, 1, len(args) - 1) = 1 len(args) c = mid(args, i, 1) if c = "(" pdepth = pdepth + 1 elseif c = ")" pdepth = pdepth - 1 elseif c = "," , pdepth = 0 c = "[[,]]" end if safeargs = safeargs & c next args = split(safeargs, "[[,]]") getparametern = trim(args(n - 1)) end function
for example,
sub test() dim long = 1 3 debug.print getparametern("f(x,g(x,y,z),z)", i) next end sub
produces:
x g(x,y,z) z
i see no reason make function volatile.
Comments
Post a Comment