+ Reply to Thread
Results 1 to 5 of 5

Thread: Text to Column with Text & Varying-length Numbers

  1. #1
    Registered User
    Join Date
    09-12-2011
    Location
    Washington DC
    MS-Off Ver
    Excel 2003
    Posts
    2

    Post Text to Column with Text & Varying-length Numbers

    I am trying to separate a string into two columns, however, the string contains text and numbers and it does not have an easy deliminator.

    Here is an example of the string data:
    Antioxidant Rich Fruit Blends. 6 months and up4.00 oz
    4.22 oz
    10.00 oz
    Snap on lid 3.5 oz - 2 pack7.00 oz
    No artificial flavors. Microwavable.45.00 oz

    This is what I'm trying to get it to look like:
    A1: Antioxidant Rich Fruit Blends. 6 months and up   B1: 4.00 oz
    A1:                                                  B1: 4.22 oz
    A1:                                                  B1: 10.00 oz
    A1: Snap on lid 3.5 oz - 2 pack                      B1: 7.00 oz
    A1: No artificial flavors. Microwavable.             B1: 45.00 oz
    The only consistent aspect is the "oz" at the end, however, as you can see sometimes "oz" appears in the middle of the string.

    I have tried LEFT/RIGHT/Mid, however, since the numbers I want in column B vary in length from 4 characters to 6, LEFT/RIGHT/MID ends up cutting something wrong.

    Thanks for your help!
    Last edited by vtshipe; 09-12-2011 at 05:49 PM.

  2. #2
    Registered User
    Join Date
    09-12-2011
    Location
    Washington DC
    MS-Off Ver
    Excel 2003
    Posts
    2

    Talking Re: Text to Column with Text & Varying-length Numbers

    After giving this problem a little space, I was able to figure out a solution.

    =MID(A1,IF(LEN(A1)<10,1,MIN(FIND({"0.",1,2,3,4,5,6,7,8,9},A1&"0.123456789",SEARCH("oz",A1,LEN(A1)-3)-8))),12)
    The key to this problem is figuring out where to tell the MID function to start. So, the MIN(FIND function finds the first occurrence of a number 1 through 9, but instead of starting to look for numbers at the beginning of the cell we find the last "oz" and back up 8 characters.

    I hope this helps someone.
    Last edited by vtshipe; 09-13-2011 at 12:21 AM. Reason: added the "0." to the FIND so it'd pickup on decimals (0.24).

  3. #3
    Valued Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389

    Re: Text to Column with Text & Varying-length Numbers

    Hi you can try this and see if it works for you.

    Sub TestSplit()
    
    ' Notes
    '------------------------------------------------------------------
    ' This only where there is "OZ" at the end of the line to be split.
    ' It works backwards from find the "OZ" and checks each character to see if it is numeric, blank or a decimal point.
    ' It allows the presents of 1 decimal point in look backwards.
    ' It ignores spaces until a letter is detected or the second decimal point.
    ' Places output in column C
    ' this does not remove or add any spaces between numbers and "OZ"
    '-------------------------------------------------------------------
    Dim OZPosition As Integer 'the counting Left to right where the last "OZ" occurs
    Dim CurrentCHAR As Variant
    
    Dim Strlength As Integer ' length of the text in A column
    Dim FLG As Boolean 'Flag used to detect first decimal point look backwards in the string.
    Dim rowSTART As Long 'Start Row
    Dim rowEND As Long ' End row
    
    CurrentCHAR = ""
    OZPosition = 0
    
    
    Range("B:C").Clear 'Optional... clear values in Columns B and C
    
    ' ENTER Number of rows to do this on.
    rowSTART = InputBox("Enter Start Row number", "START ROW NUMBER")
    rowEND = InputBox("Enter End Row number", "END ROW NUMBER")
    
    
    For J = 1 To rowEND ' used to cycle through the rows using variable J
    
    
    'set default values for each row
    ozp = 0
    FLG = False 'set to false for start of eachsearch in the string
    CurrentCHAR = "´"
    OZPosition = 0
    Strlength = Len(Range("A" & J).Text)
    OZPosition = InStrRev(UCase(Range("A" & J)), "OZ") 'look backwards in the string for "OZ"
    
        If OZPosition >= 0 Or Len(Range("A" & J)) Then     'Abort if the Postion of " OZ" is less than 0 or cell is empty
        
                    For ozp = OZPosition - 1 To 1 Step -1 'cycle backwards through the characters from the postion of Oz in the value from column A
                    
                      
                    
                    CurrentCHAR = Mid(Range("A" & J), ozp, 1) ' Get the character you want to check.
                   
                    
                          
                        If FLG = True And CurrentCHAR = "." Then
                            Cells(J, "C").Value = Right(Range("A" & J), OZPosition - ozp + 1)
                        
                            Exit For ' exit search if a second ¨.¨is found
                        Else
                                    If CurrentCHAR = " " Or CurrentCHAR = "." Then
                                        
                                        Cells(J, "C").Value = Right(Range("A" & J), OZPosition - ozp)
                                        
                                        If CurrentCHAR = "." Then FLG = True ' FLG true if decimal is detected.
                                               
                                    Else
                                                        If IsNumeric(CurrentCHAR) Then
                                                            
                                                            Cells(J, "C").Value = Right(Range("A" & J), OZPosition - ozp + 2)
                                                            
                                                        Else
                                                            
                                                            Cells(J, "C").Value = Right(Range("A" & J), OZPosition - ozp + 1)
                                                            
                                                        Exit For ' Exit loop if a non-numeric is detected.
                                                        End If
                                    End If
                                        
                            End If
                          
                                Debug.Print ozp & "  C VALUE " & Cells(J, "C").Value
                        
                    
                    Next ozp
        Else
        End If
    
                
    Next J
    
    
    End Sub
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

  4. #4
    Valued Forum Contributor
    Join Date
    07-20-2011
    Location
    mysore
    MS-Off Ver
    Excel 2003
    Posts
    421

    Re: Text to Column with Text & Varying-length Numbers

    Select the First cell containing data and run macro "TxtToCol" below.Pl convey your result.
    Thanks in advance.


    Sub TxtToCol()
    'Select the starting cell that contains data and run macro
    
    Dim k, k1, k2 as string
    Dim R_ofset, L as integer
    
    R_ofset = 0
    Do While ActiveCell.Offset(R_ofset, 0).Value <> ""
    
    k = Trim(ActiveCell.Offset(R_ofset, 0).Value)
    L = Len(k)
    For T = L - 6 To 1 Step -1
    M = IsNumeric(Mid(k, T, 1))
    
    If M = False Then
    k1 = Left(k, T)
    k2 = Right(k, L - T)
    ActiveCell.Offset(R_ofset, 0).Value = k1
    ActiveCell.Offset(R_ofset, 1).Value = k2
    Exit For
    End If
    
    If T = 1 Then
    ActiveCell.Offset(R_ofset, 0).Value = ""
    ActiveCell.Offset(R_ofset, 1).Value = k
    End If
    
    Next T
    R_ofset = R_ofset + 1
    Loop
    End Sub
    Last edited by kvsrinivasamurthy; 09-13-2011 at 07:36 AM.

  5. #5
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,213

    Re: Text to Column with Text & Varying-length Numbers

    Another way:
    Attached Files Attached Files
    "Relax. What is mind? No matter. What is matter? Never mind!"

+ Reply to Thread

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