vba - Creating a function in Excel that assigns a string to certain other strings found in the workbook cells -
i trying create functions in excel assign name of municipality smaller areas called quaternaries. in sheet called "municipality sorted" have column of municipalities , quaternaries listed right of municipalities in separate cells. functions defined below in vba module not give output, rewrite formula typed in, in =localmun(s74)
. know how change row , column reference of cell in code linking values variable, if possible. first time @ creating function, made code , hoped work.
function localmun(quaternary string) dim integer = 1 dim j integer j = 5 j = 5 39 = 1 37 if (quaternary = sheets("municipality sorted").rjc(1 + i)) localmun = sheets("municipality sorted").rjc1 else: next next j end function
function districtmun(localmun string) dim k integer k = 5 k = 5 9 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r4c1 next k k = 10 14 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r11c1 next k k = 15 18 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r16c1 next k k = 19 27 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r19c1 next k k = 28 33 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r28c1 next k k = 35 40 if (localmun = sheets("municipality sorted").rkc1) districtmun = sheets("municipality sorted").r34c1 end function
edit
i tried using paul bica's code, did not work:
the function not output string, displays typed in.
i'm not sure understand functions supposed can work specific cells references in code bellow
.
option explicit function localmun(quaternary string) dim long dim j long sheets("municipality sorted") = 5 39 step 1 'initialized 5; incremented 1 after iteration j = 1 37 'the "step 1" implied; can "step -5" if quaternary = .cells(i, j + 1).value localmun = .cells(i, j).value 'reference cell(row, col) exit end if next next end end function
function districtmun(byval lmun string) dim long 'this "visible" in function sheets("municipality sorted") = 5 9 if lmun = .cells(i, 1) districtmun = .cells(4, 1) next = 10 14 if lmun = .cells(i, 1) districtmun = .cells(11, 1) next = 15 18 if lmun = .cells(i, 1) districtmun = .cells(16, 1) next = 19 27 if lmun = .cells(i, 1) districtmun = .cells(19, 1) next = 28 33 if lmun = .cells(i, 1) districtmun = .cells(28, 1) next = 35 40 if lmun = .cells(i, 1) districtmun = .cells(34, 1) next end end function
usually declare variables @ top of functions
Comments
Post a Comment