• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

[Solved] Can't split a string the way i want

shahin

Active Member
Hi there all! Hope you are doing well. I got entangled in an issue and couldn't solve it the way i want. I used a string in my code and wanted to get the last part of it using SPLIT and UBOUND function. I made it the way most people tend to avoid. If anyone out there helps me accomplish it in an accurate and succinct way, I would be very happy. Thanks in advance.

EDIT: I used split and ubound function two times here to get the result but my intention is to get the same result using split and ubound function once.

String I have used in Range("A3")= "Harry potter and-the order of phoenix"
First Seperator="-"
Second Seperator=" "
End result= "phoenix"

Here is the code I used:


Code:
Sub Splitting()

Dim str As Variant, w As Variant
Dim v As String, m As String, s As String

s = Range("A3").Value

str = Split(s, "-")

v = str(UBound(str))

w = Split(v, " ")

m = w(UBound(w))

Debug.Print m

End Sub
 
Last edited:

As written in Split VBA inner help, space is the default delimiter.

So when delimiter is not a space, you must specify it in the function …​
 
Back
Top