We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Error in Function

thenone
thenone asked
on
Medium Priority
185 Views
Last Modified: 2010-04-07
With this function im getting an error

Public Function Get1Words(sInput As String) As String
    Dim sTmp() As String
   
    sTmp() = Split(sInput, " ", 2)
    sTmp(UBound(sTmp)) = vbNullString <<<sub script is out of range
    Get1Words = Join(sTmp, " ")
End Function


Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
It doesn't error for me...

Option Explicit

Private Sub Command1_Click()
    Dim source As String
   
    source = "the big fat cat ate fish"
   
    Dim dest As String
    dest = Get1Words(source)
   
    Debug.Print source
    Debug.Print dest
End Sub

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




Output:

the big fat cat ate fish
the

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Could it be that I am putting it in a module
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
That wouldn't make any difference to this code...

Author

Commented:
well thats weird I am getting an error sub script is out of range
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Create a new project and paste the code above...do you still get an error?

Author

Commented:
no i don't that is really weird.
Commented:
Ensure that you are passing it some value to sInput. An empty string will give you a subscript out of range error.

Author

Commented:
ok found the problem the input string was null.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.