• 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.

Can't fix errors caused by scooping value from space-separated strings

shahin

Active Member
I've written a macro to scoop certain portion from strings separated by space. It was doing good until there is no such match found in certain strings. If the macro doesn't find any string meeting the aforesaid criteria within the defined Range then it breaks. How can I fix this error from happening. I tried to take the value separated by second last space.

If we consider the following example as a test then my macro will break when it hits the cell containing "Keh Pan" because I intended to pick the value separated by second last space and place those to its adjacent cell.

This is where I'm applying my macro:

Code:
Margaret C Hernon Tr
Wdg Cambridge St Rt
Keh Pan
Li Tan Pham

This is how I wanted the output to be (see the adjacent column):

Code:
Margaret C Hernon Tr    C
Wdg Cambridge St Rt     Cambridge
Keh Pan              'code breaks here
Li Tan Pham

The macro I'm trying with:
Code:
Sub separate_certain_portions()
    Dim r As Range, pstr As Variant
  
    For Each r In Range("A1:A4")
        pstr = Split(r)
'        If pstr(UBound(pstr) - 2) <> "" Then
            r(1, 2) = pstr(UBound(pstr) - 2)
'        End If
    Next r
End Sub
 
May be
Code:
Sub Separate_Certain_Portions()
    Dim r  As Range
    Dim x  As Long

    For Each r In Range("A1:A4")
        x = InStr(r, " ") + 1
        r(1, 2) = Split(Mid(r, x))(0)
    Next r
End Sub
 
It worked against above strings. However, it will give wrong values when the string is:

"Margaret C Hernon Tr Lt Hwr"

If I run your suggested macro, I will get "C" whereas I'm expecting "Tr".
 
I don't get your logic here .. I thought you need the second part only
Can you explain the logic .. why "Tr" is expected here?
 
I am trying to scoop the portion separated by second last space. From this string:

"Margaret C Hernon Tr Lt Hwr":

Last space separated value = "Lt"
Second last space separated value = "Tr"
Third last space separated value = "Hernon"
 
Nothing from the above string as it doesn't meet the criteria. Btw, the macro I've pasted above can deal with that If I use "On error resume next". However, I hate to use that line.
 
Try this code
Code:
Sub Separate_Certain_Portions()
    Dim y  As Variant
    Dim r  As Range
    Dim x  As Long

    For Each r In Range("A1:A5")
        x = InStrRev(r, " ") + 1
        y = UBound(Split(Trim(Mid(r, 1, x - 1)))) - 1

        If UBound(Split(r, " "), 1) = 1 Then
            r(1, 2) = ""
        Else
            r(1, 2) = Split(Trim(Mid(r, 1, x - 1)))(y)
        End If
    Next r
End Sub
 
Yes, it is working. Thanks. However, can you tell me where I was going wrong with the first macro I've pasted above?
 
Yes, that is. I used a conditional statement to avoid any such string not fulfilling the criteria, but it didn't work.
 
If you would like to put conditional statements you have to calculate all the possibilities .. and manipulate the string according
 
These conditional statements is so simple not difficult at all ..Try using F8 while running the code to run the code line by line .. I have learned a lot through this approach
 
You only needed to test the UBound of pstr, remembering that it is indexed from 0:

Code:
        pstr = Split(r)
        If UBound(pstr) > 1 Then
           r(1, 2) = pstr(UBound(pstr) - 2)
        End If
 
Back
Top