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:
This is how I wanted the output to be (see the adjacent column):
The macro I'm trying with:
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