Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Quick Access/VBA string function needed

Posted on 2013-12-20
4
Medium Priority
?
518 Views
Last Modified: 2013-12-20
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
Comment
Question by:terpsichore
4 Comments
 
LVL 58
ID: 39731599
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
 
LVL 49

Expert Comment

by:Dale Fye
ID: 39731618
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 39731630
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
 

Author Closing Comment

by:terpsichore
ID: 39731650
perfect. thanks so much.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question