+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Macro to separate data

    I'm looking for a macro that can take data that is in one cell with hard returns between the fields and separate it into columns. I need like data in the same columns. For example, my cells might look like this:

    "EXISTING INFO
    Route 11
    BFO576(R)
    3.6 kft"

    I need 4 columns from this data, but the next cell might only have 3 pieces of that information, but I need all of the "ROUTE" data in one column, all of the "BFO" data in a column and all of the "kft" in one column so that I can sort by any piece of that puzzle.

    How could I do this if all cells don't contain all 4 pieces of the puzzle?

    Many thanks in advance!

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,133

    Re: Macro to separate data

    Welcome to the forum.

    Post a workbook with some examples of the data.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-10-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to separate data

    Here is a workbook example. Thanks so much!
    Attached Files Attached Files

  4. #4
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,133

    Re: Macro to separate data

    Try this:
    Code:
    Sub x()
        Dim cell As Range
        
        Columns("A:A").TextToColumns _
                Destination:=Range("A1"), _
                DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, _
                ConsecutiveDelimiter:=False, _
                Tab:=False, _
                Semicolon:=False, _
                Comma:=False, _
                Space:=False, _
                Other:=True, _
                OtherChar:=vbLf
        
        For Each cell In Intersect(ActiveSheet.UsedRange, Range("A:C"))
            If cell.Text Like "*kft" And cell.Column <> 4 Then
                Cells(cell.Row, 4) = cell.Value
                cell.ClearContents
            ElseIf cell.Text Like "BFO*" And cell.Column <> 3 Then
                Cells(cell.Row, 3) = cell.Value
                cell.ClearContents
            ElseIf cell.Text Like "Route*" And cell.Column <> 2 Then
                Cells(cell.Row, 2) = cell.Value
                cell.ClearContents
            End If
        Next cell
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    03-10-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to separate data

    That worked beautifully - except for the cells with three lines in them - they skipped the BFO sections when they separated - any idea why?
    Last edited by MyBabyO; 03-10-2010 at 03:10 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0