excel - VBA passing variable to function -
final goal: trying edit existing text in column upper case proper (upper case)- exception whereby if word follows hyphen will-remain lower case. outputting 2 columns across.
my first sub needs pass active cell function applies formatting (function tested , works).
i don't know how make work.
option explicit dim txt string dim long dim strtest string dim strarray() string dim lcaseon boolean dim firstrow long, startit long dim thiscell range dim lastrow long sub throughcols() dim thiscell range datarange startit = firstrow + 1 = startit lastrow ' no idea how pass cell picking through function thiscell = sheets("names").select: range("b" & i).select arraymanip (thiscell) 'i need pass activecell arraymanip- not sure how next end sub '==================================================================== function arraymanip(thiscell) 'hard coded source cell testing ' trying itterate through column ' clear out data erase strarray txt = "" 'set default case lcaseon = false ' string array using " " separator strtest = worksheetfunction.proper(activecell.value) strtest = replace(strtest, "-", " - ") strarray = split(strtest, " ") ' itterate through array looking make text foll = lbound(strarray) ubound(strarray) if strarray(i) = "-" lcaseon = true goto nextiteration end if if lcaseon strarray(i) = lcase(strarray(i)) lcaseon = false nextiteration: end if ' loop through array , build text string output message box txt = txt & strarray(i) & " " ' remove space txt = trim(replace(txt, " - ", "-")) activecell.offset(0, 2).select: activecell.value = txt next ' msgbox txt end function '==================================================================== sub datarange() sheets("names").columns("b") if worksheetfunction.counta(.cells) = 0 '<--| if no data whatever msgbox "sorry: no data" else .specialcells(xlcelltypeconstants) '<--| reference cells constant (i.e, not derived formulas) values) firstrow = .areas(1).row lastrow = .areas(.areas.count).cells(.areas(.areas.count).rows.count).row end ' msgbox "the first row " & firstrow ' msgbox "last row " & lastrow end if end end sub
instead of:
thiscell = sheets("names").select: range("b" & i).select do:
'## use set keyword assign object variable set thiscell = sheets("names").range("b" & i) also, instead of relying on activecell in function body, change to:
strtest = worksheetfunction.proper(thiscell.value) then call function:
call arraymanip(thiscell) if function doesn't return value caller, should sub instead. change sub , above call statement should still work.
see also:
Comments
Post a Comment