Search for the first space using instr

G

Guest

How can I return the first space in a string using instr function.
Using the following I get the error 'invalid procedure call or argument'

However it works for other characters ok.

Bruce

Function myProduct(a As String)
myProduct = CStr(Left(a, InStr(a, " ") - 1))
End Function
 
D

Douglas J. Steele

Assuming that there actually is a blank character in the string, that should
work. If there isn't a blank, InStr will return 0, so that you will have an
error.

If it's possible not to have blanks, try:

Function myProduct(a As String) As String
Dim lngPos As Long

lngPos = InStr(a, " ")
If lngPos > 0 Then
myProduct = CStr(Left(a, lngPos - 1))
Else
myProduct = a
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top