Consulting

Results 1 to 12 of 12

Thread: Break at First Number in String

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Break at First Number in String

    I want to break up a cell into two cells (like text-to-columns), using the space just before the first number in the cell as the delimiter.

    So:

    John B. Smith 34 South 3rd Street Quakertown 18951

    Turns into:

    Cell 1: John B. Smith
    Cell 2: 34 South 3rd Street Quakertown 18951
    ~Anne Troy

  2. #2
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    This should do the trick for a single cell. A column might take me a little longer due to my lacking knowledge of excel Or one of ther true excel experts on this forum, very little time at all.


    Sub SplitStringAtFirstNumber()
    Dim LoopInt As Integer
    Dim byteArray() As Byte
    Dim Flag As Boolean
      'converting cell text into ascii values
      byteArray = StrConv(Range("A1").Value, vbFromUnicode)
      'Arrays created with StrConv are by default 0 based
      'so LoopInt is set to 0
      LoopInt = 0
      'set the flags so that we know where the first number encountered is
      Flag = True
      While Flag = True
        'when you encounter the first number, end check with flag
        If byteArray(LoopInt) > 47 And byteArray(LoopInt) < 58 Then
          Flag = False
        End If
        LoopInt = LoopInt + 1
      Wend
      'this loop adds one too many so we subtract one to get the right value
      'for the left and right functions
      LoopInt = LoopInt - 1
      'Take first string and paste it into the ajoining cell
      'Using left with looping as the marker of the space in the string
      Range("B1").Value = Left(Range("A1").Value, LoopInt)
      'First change loopint around so that it will take the rest of the
      'text in the cell after the space
      LoopInt = Len(Range("A1").Value) - LoopInt
      'Now take the second string and paste it into the next cell
      'using the right function
      Range("C1").Value = Right(Range("A1").Value, LoopInt)
      'Which still leaves the original value in the first cell
    End Sub
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This will work for the whole column.

    Option Explicit
    
    Sub Macro1()
    Dim i               As Long
    Dim j               As Long
    Dim k               As Long
    Dim n               As Long
    Dim LastRow         As Long
        LastRow = Range("A65536").End(xlUp).Row
        For i = 1 To LastRow
            k = 0
            n = 0
            For j = 0 To 9
                k = InStr(1, Range("A" & i).Text, j)
                If k > 0 Then
                    If n = 0 Then
                        n = k
                    Else
                        n = Application.WorksheetFunction.Min(n, k)
                    End If
                End If
            Next j
            If n > 0 Then
                Range("B" & i).Value = _
                Mid(Range("A" & i).Text, n, Len(Range("A" & i).Text))
    Range("A" & i).Value = _
                Left(Range("A" & i).Text, n - 2)
            End If
        Next i
    End Sub

  4. #4
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    That's beautiful, Jake.

    I got an error on the very last range line, but it still performed beautifully, as though the last range line thang isn't needed.
    ~Anne Troy

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    And here's a way to do it without code.

    If your original data is in A2, then

    In B2 array-enter (Ctrl+Shift+Enter)
    =MIN(IF(ISERR(FIND({0,1,2,3,4,5,6,7,8,9},A2)),LEN(A2)+1,FIND({0,1,2,3,4,5,6,7,8,9},A2)))

    In C2 enter
    =LEFT(A2,B2-1)
    In D2 enter
    =MID(A2,B2,LEN(A2))
    Copy down as far as your data and, if you like, Copy and Paste Special > Values columns C and D to themselves and delete column B.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    egads that's ugly.
    LOL!!

    But I like your thinking, Tony.
    ~Anne Troy

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Dreamboat
    egads that's ugly.
    LOL!!

    But I like your thinking, Tony.
    But much more elegant and quicker than a looped macro.

  8. #8
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Anne, which line errored for you? Can you post the workbook or email it to me?

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    This one:
    Range("A" & i).Value = _ 
                Left(Range("A" & i).Text, n - 2)
    But don't worry about it; it works. I'll send you the workbook later if you need it.

    and I never got notifs of yours or Zack's post

    k.
    It's erroring when there's a zero as the first letter (which can happen 'cause we scanned the docs). They should be O's.
    ~Anne Troy

  10. #10
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ok, I see. Try this.

    k = InStr(2, Range("A" & i).Text, j)

  11. #11
    VBAX Regular Niko's Avatar
    Joined
    Apr 2005
    Posts
    8
    Location
    Hi Dreamboat,

    Just check out the following link:

    http://www.cpearson.com/excel/FirstLast.htm

    This may be of ur help.

    Cheers
    Niko

  12. #12
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Actually, it's over 3000 records, and Jake's (drj) code works absolutely perfect. It fails if the first character in the cell is a number, but that's GOOD.

    These addresses were scanned. I'm not trying to separate names, but addresses from names.

    Marking it solved.
    ~Anne Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •