Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

get 8 words in a string

Posted on 2006-03-27
3
Medium Priority
?
182 Views
Last Modified: 2010-05-01
I am trying to get the first 8 words from a string with this function I get sub string error.

Public Function Get8Words(sInput As String) As String
    Dim sTmp() As String
   
    sTmp() = Split(sInput, " ", 9)
    sTmp(UBound(sTmp)) = vbNullString<<<<<< Error here
    Get8Words = Join(sTmp, " ")
End Function
0
Comment
Question by:thenone
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 16307861
Public Function Get8Words(sInput As String) As String
    Dim sTmp() As String
   
    sTmp() = Split(sInput, " ")
    redim preserve sTmp(0 to 7) as string
    Get8Words = rtrim(Join(sTmp, " "))
End Function
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 total points
ID: 16307867
Same thing...works for me...

Option Explicit

Private Sub Command1_Click()
    Dim source As String
   
    source = "the big fat cat ate fish and purred very loudly"
   
    Dim dest As String
    dest = Get8Words(source)
   
    Debug.Print source
    Debug.Print dest
End Sub

Public Function Get8Words(sInput As String) As String
    Dim sTmp() As String
   
    sTmp() = Split(sInput, " ", 9)
    sTmp(UBound(sTmp)) = vbNullString
    Get8Words = Join(sTmp, " ")
End Function



Output:

the big fat cat ate fish and purred very loudly
the big fat cat ate fish and purred

0
 
LVL 3

Accepted Solution

by:
ptan218 earned 200 total points
ID: 16307911
The same code work for me as well. Maybe you should check what is the sInput string.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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