Excel VBA Sentence Case Funtion Needs Fine Tuning -
below function built others changes text sentence case (first letter of each sentence capitalized). function works nicely except doesn't capitalize first letter of first word. issue if sentence entered in caps, function nothing. i'm looking assistance in tweaking function correct these issues.
option explicit function propercaps(strin string) string dim objregex object dim objregmc object dim objregm object set objregex = createobject("vbscript.regexp") strin = lcase$(strin) objregex .global = true .ignorecase = true .pattern = "(^|[\.\?\!\r\t]\s?)([a-z])" if .test(strin) set objregmc = .execute(strin) each objregm in objregmc mid$(strin, objregm.firstindex + 1, objregm.length) = ucase$(objregm) next end if end propercaps = strin end function
thanks, gary
i renamed function sentencecase() , made few more adjustments:
public function sentencecase(byval str string) string dim regex object, regexm object, indx object, indxs object set regex = createobject("vbscript.regexp") str = replace$(str, vbnullchar, vblf) str = replace$(str, vbback, vblf) str = ltrim$(lcase$(str)) regex .ignorecase = true .multiline = true .global = true .pattern = "(^|[\n\f\r\t\v\.\!\?]\s*)(\w)" if .test(str) set indxs = .execute(str) each indx in indxs mid$(str, indx.firstindex + 1, indx.length) = ucase$(indx) next end if end sentencecase = str end function
this tested with:
msgbox sentencecase(" upper case sentence." & _ vbcrlf & "next line!next sentence" & _ vbcr & "cr ! lower case" & _ vblf & "lf .new sentence" & _ vbnullchar & " null?null char" & _ vbnullstring & "nullstring spaces" & _ vbtab & "tab char.ttt" & _ vbback & "back? char" & _ vbformfeed & "ff ff words" & _ vbverticaltab & "vertical tab.| lower .case words")
results:
you can find more details here: microsoft - regular expressions
Comments
Post a Comment