1

I have a cell text "12-3 0000 9 FLY AIR Make MY Trip" and I want the output like 12-3 0000 9 into next cell and the next sequence cell "FLY AIR Make MY Trip".

Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String
    'Updateby20150306
    Dim xLen As Long
    Dim xStr As String

    xLen = VBA.Len(pWorkRng.Value)

    For i = 1 To xLen
        xStr = VBA.Mid(pWorkRng.Value, i, 1)
        If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
            SplitText = SplitText + xStr
        End If
    Next
End Function

=SplitText(A2,FALSE) into a blank cell to get the only the text.

Result as 12300009

=SplitText(A2,TRUE) into a blank cell to get the only the number.

Result as "- FLY AIR Make MY Trip"

5
  • Is the format always NUMBERS (and "-") followed by TEXT Commented Nov 7, 2017 at 6:09
  • Please clarify the question Commented Nov 7, 2017 at 6:54
  • You have posted insufficient test data to reach conclusive solution. Commented Nov 7, 2017 at 7:01
  • "12-3 0000 9 FLY AIR Make MY Trip" i wanna to split into "12-3 0000 9" and "FLY AIR Make MY Trip" Commented Nov 7, 2017 at 7:14
  • @VinothNarayan can you post few more lines of data? Commented Nov 7, 2017 at 8:14

3 Answers 3

1

Assume you want to get the numeric first part, if your parameter pIsNumberis set true:

Option Explicit
Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String
'Declare ALL variables
Dim xLen As Long
Dim xStr As String
Dim i As Integer
xLen = VBA.Len(pWorkRng.value)
For i = 1 To xLen
    xStr = VBA.Mid(pWorkRng.value, i, 1)
     If pIsNumber And InStr(" -0123456789", xStr) Then
        SplitText = SplitText & xStr   ' use ampersand "&" instead of "+"

     ElseIf Not pIsNumber And InStr("-123456789", xStr) = 0 Then
        SplitText = SplitText & xStr
     End If
Next
If pIsNumber Then SplitText = Replace(SplitText, String(3, " "), " ")
End Function

Note

Use ampersand "&" instead of "+" to join string values. BTW, don't forget to declare your variables and always use Option Explicit in the declaration head of your code module.

Good luck.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot it works.but for text its coming along with ""000 FLY AIR Make MY Trip".for number it works perfectly
I tested above code again and it works for me. Suppose you omitted the blank space character " " in the comparation string of the first condition: If pIsNumber AND Instr(" -0123456789", xStr) Then, whereas the comparation string in the Else condition starts without blank.
1

I would use Split, check for text and then join again:

Function SplitText(Txt As String, Optional Number As Boolean = True)
Dim Arr, outarr() As String, I As Integer
Dim nbr As String, rest As String
Arr = Split(Txt, " ")
ReDim outarr(UBound(Arr))
    For I = LBound(Arr) To UBound(Arr)
        outarr(I) = Arr(I)
        If Not IsNumeric(Left(Arr(I), 1)) Then
            ReDim Preserve outarr(I - 1)
            nbr = Join(outarr, " ")
            rest = Mid(Txt, Len(nbr) + 2)
            Exit For
        End If
    Next I
    SplitText = IIf(Number, nbr, rest)
End Function

Comments

1

Assuming your text is in cell A2

Enter following formula where you need numeric portion (to be entered as array formula i.e. CTRL+SHIFT+ENTER and not just ENTER):

=LEFT(A2,MIN(IFERROR(SEARCH(CHAR(ROW($A$65:$A$90)),A2,1),2^15))-1)

If applied correctly Excel will show surrounding braces {}.

Then assuming you have above formula in cell B2, its result can be used as output to get the remaining text part like below:

=SUBSTITUTE(A2,B2,"")

If it works then you will not require VBA.

2 Comments

It returns me empty
@VinothNarayan I am not sure how you are using it. See attached file for implementation. Example File

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.