+ Reply to Thread
Results 1 to 4 of 4

VBA script to parse string variables in column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    VBA script to parse string variables in column

    Hi Everyone,

    I've researched to the point of needing help now. Any help would be appreciated.

    I have a column that is called Options with a string of words (a chunk of HTML really). In this column I want to parse through each cell and look for each of the following variables:

    Draw Hand: (Right,Left)
    Draw Weight: (x)
    Draw Length: (in half inches)
    Arrow Length: (in half inches)

    If any of those variable strings are in the cell, then I want them placed in a separate column in the spreadsheet.

    Spreadsheet here at Google Docs:

    I would ideally like to have a VBA script that is flexible to add more variable strings as needed. Many thanks for your help!

    Kind Regards,
    Justin

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA script to parse string variables in column

    I like to use normal worksheet formulas to do the brunt of the work, so you can run this macro without the last two lines of code to see the formulas that are created. It might work as is if you simply add another column and copy the formula from column F over...
    Option Explicit
    
    Sub SplitText()
    'Jerry Beaucaire   4/19/2010
    Dim LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("B:B").WrapText = True
    
    'add formulas in all cells
    Range("C2:C" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC2)), MID(RC2,SEARCH(R1C,RC2)+LEN(R1C)+2,SEARCH(""<"",RC2,SEARCH(R1C,RC2)+LEN(R1C)+2)-(SEARCH(R1C,RC2)+12)), """")"
    Range("D2:D" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC2)), MID(RC2,SEARCH(R1C,RC2)+LEN(R1C)+2,SEARCH(""<"",RC2,SEARCH(R1C,RC2)+LEN(R1C)+2)-(SEARCH(R1C,RC2)+14)), """")"
    Range("E2:E" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC2)), MID(RC2,SEARCH(R1C,RC2)+LEN(R1C)+2,SEARCH(""<"",RC2,SEARCH(R1C,RC2)+LEN(R1C)+2)-(SEARCH(R1C,RC2)+14)), """")"
    Range("F2:F" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC2)), MID(RC2,SEARCH(R1C,RC2)+LEN(R1C)+2,SEARCH(""<"",RC2,SEARCH(R1C,RC2)+LEN(R1C)+2)-(SEARCH(R1C,RC2)+14)), """")"
        
    Range("C2:F" & LR).Value = Range("C2:F" & LR).Value     'removes the formulas
    Range("C:F").Columns.AutoFit                            'tidies up appearance
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    04-19-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: VBA script to parse string variables in column

    Well done Jerry! Thank you for helping me with this. Does anyone have any thoughts on how we could automatically clean up the data once its been parsed into the new fields? Would like to continue working in the script Jerry has framed up to this point.

    Here's how the data looks after its been parsed:
    Draw Hand	Draw Weight	Draw Length	Arrow Length	Arrow Size	String Length
    Right = $0.00					
    Right Hand = $0.00					
    Right Hand = $0.00	60-lbs = $0.00	28.5 inch = $0.00			
    Right Hand = $0.00	45-lbs = $0.00				
    Right Hand = $0.00					
    Right = $0.00					
    Right Hand = $0.00	45-lbs = $0.00
    For the columns the fields should allow for:
    Draw Hand: (Right, Left) - 5 char max
    Draw Weight: (60) - 2 char max
    Draw Length: (28.5) - 4 char max
    Arrow Length: (27.5) - 4 char max
    Arrow Size: (3000) - 4 char max
    String Length: (104) - 3 char max


    Current VBA script:
    Sub SplitOptionHTMLText()
    '
    ' Lables the columns for the parse to work below
    '
        Range("P1").Select
        ActiveCell.FormulaR1C1 = "Draw Hand"
        Range("Q1").Select
        ActiveCell.FormulaR1C1 = "Draw Weight"
        Range("R1").Select
        ActiveCell.FormulaR1C1 = "Draw Length"
        Range("S1").Select
        ActiveCell.FormulaR1C1 = "Arrow Length"
        Range("T1").Select
        ActiveCell.FormulaR1C1 = "Arrow Size"
        Range("U1").Select
        ActiveCell.FormulaR1C1 = "String Length"
    
        
    ' Help from Jerry Beaucaire   4/19/2010
    Dim LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("O:O").WrapText = True
    
    'add formulas in all cells
    Range("P2:P" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+12)), """")"
    Range("Q2:Q" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
    Range("R2:R" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
    Range("S2:S" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
    Range("T2:T" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
    Range("U2:U" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH(""<"",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
                
        
    Range("P2:U" & LR).Value = Range("P2:U" & LR).Value     'removes the formulas
    Range("P:U").Columns.AutoFit                            'tidies up appearance
    
    End Sub
    Thank you,
    Justin

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA script to parse string variables in column

    Here you go:
    Option Explicit
    
    Sub SplitOptionHTMLText()
    Application.ScreenUpdating = False
    
    ' Labels the columns for the parse to work below
        Range("P1") = "Draw Hand"
        Range("Q1") = "Draw Weight"
        Range("R1") = "Draw Length"
        Range("S1") = "Arrow Length"
        Range("T1") = "Arrow Size"
        Range("U1") = "String Length"
        
    ' Help from Jerry Beaucaire   4/19/2010
    Dim LR As Long
    
    LR = Range("O" & Rows.Count).End(xlUp).Row
    Columns("O:O").WrapText = True
    
    'add formulas in all cells
    Range("P2:P" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,5), """")"
    Range("Q2:Q" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,2), """")"
    Range("R2:R" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH("" "",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+13)), """")"
    Range("S2:S" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH("" "",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+13)), """")"
    Range("T2:T" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH("" "",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+12)), """")"
    Range("U2:U" & LR).FormulaR1C1 = _
        "=IF(ISNUMBER(SEARCH(R1C,RC15)), MID(RC15,SEARCH(R1C,RC15)+LEN(R1C)+2,SEARCH("" "",RC15,SEARCH(R1C,RC15)+LEN(R1C)+2)-(SEARCH(R1C,RC15)+14)), """")"
                
        
    Range("P2:U" & LR).Value = Range("P2:U" & LR).Value     'removes the formulas
    Range("P2:U" & LR).HorizontalAlignment = xlCenter       'tidies up appearance
    Range("P:U").Columns.AutoFit                            'tidies up appearance
    
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1