Home |
Search |
Today's Posts |
#22
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I like the "split at last space" condition 'challenge'. A little more
"wordy" than your regex solution, but still a one-liner (my own self-imposed restriction) VBA function... Function Truncate(ByVal Source As String, _ Optional TrimAt As Long = 30000) As String Truncate = RTrim$(Left(Source, InStrRev(Left(Source, TrimAt), ".") - _ (InStrRev(Left(Source, TrimAt), " ")) * _ (Not Left(Source, TrimAt) Like "*.*") - _ (Len(Left(Source, TrimAt))) * _ (Not Left(Source, TrimAt) Like "*[. ]*"))) End Function Note that I still left the optional TrimAt argument so the OP can set it to 500 (or any other value desired) in the calling formula. And this is how I would write the function in "real life" (that is, without forcing it to a one-liner)... Function Truncate(ByVal Text As String, _ Optional TrimAt As Long = 30000) As String Text = Left$(Text, TrimAt) Truncate = Left$(Text, InStrRev(Text, ".")) If Len(Truncate) = 0 Then Truncate = RTrim(Left(Text, InStrRev(Text, " "))) If Len(Truncate) = 0 Then Truncate = Text End Function Note: I changed some argument names to avoid using line continuations Damn! The line wrapped at an 'unfortunate' spot (a blank) anyway. Here is the same function renamed to Chop instead of Truncate; it should have each statement fit on a single line without wrapping... Function Chop(ByVal Text As String, _ Optional TrimAt As Long = 30000) As String Text = Left$(Text, TrimAt) Chop = Left$(Text, InStrRev(Text, ".")) If Len(Chop) = 0 Then Chop = RTrim(Left(Text, InStrRev(Text, " "))) If Len(Chop) = 0 Then Chop = Text End Function Rick |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
change "true" and "false" to "availble" and "out of stock" | Excel Worksheet Functions | |||
HELP on "left","right","find","len","substitute" functions | Excel Discussion (Misc queries) | |||
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next | New Users to Excel | |||
Complex if test program possible? If "value" "value", paste "value" in another cell? | Excel Discussion (Misc queries) | |||
Insert "-" in text "1234567890" to have a output like this"123-456-7890" | Excel Discussion (Misc queries) |