• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

Quick Access/VBA string function needed

Dear experts -
Hopefully a quick one - I need a function that will give me a maximum of x characters of a string fed to it, BUT not in the middle of a word (defined as ending in a space or hyphen) UNLESS there is only a single word there.
Can anyone solve this little riddle?
Thanks!
0
terpsichore
Asked:
terpsichore
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Your going to need to write this if I'm understanding correctly.

Some examples would be good before we continue just to make sure were on the same page.

Jim.
0
 
Dale FyeCommented:
So, if X characters ends in the middle of a word, what do you want, everything to the left of that word, or everything including that word?
0
 
GrahamSkanRetiredCommented:
The riddle is in interpreting your question.
Does this do it?
Function GetString(strText As String, iCharCount As Integer) As String
    Dim p As Integer
    
    strText = Replace(strText, "-", " ")
    p = iCharCount
    
    Do While Mid(strText, p, 1) <> " "
        If p = 1 Then
            GetString = Left(strText, iCharCount)
            Exit Function
        Else
            p = p - 1
        End If
    Loop
    GetString = Left(strText, p)
End Function

Open in new window

0
 
terpsichoreAuthor Commented:
perfect. thanks so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now