+ Reply to Thread
Results 1 to 9 of 9

VBA code to split cells with pipe into multiple rows

Hybrid View

lmtwashington VBA code to split cells with... 02-15-2013, 03:12 PM
mat.davies Re: VBA code to split cells... 02-15-2013, 04:10 PM
lmtwashington Re: VBA code to split cells... 02-15-2013, 04:33 PM
mehmetcik Re: VBA code to split cells... 02-15-2013, 04:37 PM
mat.davies Re: VBA code to split cells... 02-15-2013, 04:57 PM
xladept Re: VBA code to split cells... 02-15-2013, 04:50 PM
lmtwashington Re: VBA code to split cells... 02-15-2013, 04:57 PM
lmtwashington Re: VBA code to split cells... 02-15-2013, 05:00 PM
xladept Re: VBA code to split cells... 02-15-2013, 05:29 PM
  1. #1
    Registered User
    Join Date
    11-01-2012
    Location
    Silverdale, Washington
    MS-Off Ver
    Excel 2007
    Posts
    12

    VBA code to split cells with pipe into multiple rows

    I need a VBA code to split cells with pipe into multiple rows, only inserting the cells' contents into the rows inserted below. For example, I have a spreadsheet with 4 quantities split by a pipe in cell I2, and 4 prices split in a cell by a pipe in J2. I need the quantities and prices to insert respectively in the row beneath.

    Original:
    (A2)simple (I2)150|500|1000|3000 (J2)2.36|2.17|2.02|1.88
    (A3)simple (I3)500|1000|2000|3000 (J3)3.36|3.10|2.88|2.68

    To look like:
    (A2)simple (I2)150 (J2)2.36
    (A3) (I3)500 (J3)2.17
    (A4) (I4)1000 (J4)2.02
    (A5) (I5)3000 (J5)1.88
    (A6)simple (I6)500 (J6)3.36
    (A7) (I7)1000 (J7)3.10
    (A8) (I8)2000 (J8)2.88
    (A9) (I9)3000 (I9)2.68

    I'd appreciate any/all help! Thanks!

  2. #2
    Registered User
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2007/10
    Posts
    31

    Re: VBA code to split cells with pipe into multiple rows

    Hi

    I think you're best cycling over all cells and performing the following

    Check if a pipe is present
    If so, split the string into N elements using a pipe
    Add Rows if required
    Add data in rows beneath
    Loop

    So here's some code...

    currentRow=0
    for each cell in sheet.usedrange
    If cell.row <> currentRow then 
      AddedRows=0
      CurrentRow=cell.row
    End if
    If instr(1,cell,"|")>0 then
      Tmp= Split(cell,"|")
      If Ubound(tmp)+1>addedrows then
        Rows(cell.row+addedrows+1 &":" & cell.row+addedrows+ubound(tmp)+2).insert
      End if
      For I =1 to ubound(tmp)
        Cell.offset(I-1,0)=tmp(I)
      Next I
    End if
    Next cell
    I'm not at a machine with excel on at the minute, so the above might need a bit of debugging, and you should properly declare all variables. but the basic framework is there.

    Good luck

    Mat

  3. #3
    Registered User
    Join Date
    11-01-2012
    Location
    Silverdale, Washington
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code to split cells with pipe into multiple rows

    It must be missing something, because I can't get my worksheet to even think it's a macro to run. I am not a "pro" (obviously). Thanks for the help, though!

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA code to split cells with pipe into multiple rows

    Try the following

    sub macro()

    currentRow=0
    for each cell in sheet.usedrange
    If cell.row <> currentRow then
    AddedRows=0
    CurrentRow=cell.row
    End if
    If instr(1,cell,"|")>0 then
    Tmp= Split(cell,"|")
    If Ubound(tmp)+1>addedrows then
    Rows(cell.row+addedrows+1 &":" & cell.row+addedrows+ubound(tmp)+2).insert
    End if
    For I =1 to ubound(tmp)
    Cell.offset(I-1,0)=tmp(I)
    Next I
    End if
    Next cell

    end sub

  5. #5
    Registered User
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2007/10
    Posts
    31

    Re: VBA code to split cells with pipe into multiple rows

    Hi

    You just need to paste it into a sub

    below is the working code, give me a shout if you need an explanation of anything

    Sub test()
    For i = 1 To 100 'no of rows to check (allow for inserted rows)
        addedrows = 0
        For j = 1 To 10 'no of colss to check
            If InStr(1, Cells(i, j), "|") > 0 Then
                tmp = Split(Cells(i, j), "|")
                If UBound(tmp) > addedrows Then
                    Rows(i + addedrows + 1 & ":" & i + UBound(tmp)).Insert
                    addedrows = UBound(tmp)
                End If
                For k = 0 To UBound(tmp)
                    Cells(i, j).Offset(k, 0) = tmp(k)
                Next k
            End If
        Next j
    Next i
    End Sub
    Cheers

    Mat

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code to split cells with pipe into multiple rows

    Hi,

    Try this:

    Sub IMT(): Dim S As String, T As String, I(5), J(5)
    Dim h As Integer, k As Integer, n As Integer, r As Long
    r = Range("A" & Rows.Count).End(xlUp).row
    For r = 2 To r
    S = Range("I" & r): T = Range("J" & r)
    For k = 1 To 3
    h = InStr(1, S, "|"): n = InStr(1, T, "|")
    I(k) = Left(S, h - 1): S = Right(S, Len(S) - h)
    J(k) = Left(T, n - 1): T = Right(T, Len(T) - n)
    Next k
    I(k) = S: J(k) = T
    For k = 1 To 3
    Range("I" & r) = I(k): Range("J" & r) = J(k)
    Range("I" & r + 1).EntireRow.Insert: r = r + 1
    Next k
    Range("I" & r) = I(k): Range("J" & r) = J(k)
    Next r
    End Sub
    Directions for running the routine(s) just supplied

    Copy the code to the clipboard

    Press ALT + F11 to open the Visual Basic Editor.

    Open a macro-enabled Workbook or save your Workbook As Macro-Enabled

    Select “Module” the Insert menu

    Type "Option Explicit" then paste the code under it

    With the cursor between Sub and End Sub press F5 (F8 to Single Step)

    OR

    Press ALT + Q to close the code window.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Registered User
    Join Date
    11-01-2012
    Location
    Silverdale, Washington
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code to split cells with pipe into multiple rows

    It's working, but ends at row 57. There are more rows in the spreadsheet.

  8. #8
    Registered User
    Join Date
    11-01-2012
    Location
    Silverdale, Washington
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code to split cells with pipe into multiple rows

    Mat-Awesome

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code to split cells with pipe into multiple rows

    I Should have written it like this:

    For r = 2 To 4*r+1
    Sorry!
    Last edited by xladept; 02-15-2013 at 07:12 PM.

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