+ Reply to Thread
Results 1 to 6 of 6

Split data into multiple columns and rows based on specific string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Split data into multiple columns and rows based on specific string

    Hello

    It's been a while from the last visit to this great forum and touch vba things.

    I have a file contain 2 columns, ID and data ( a b c d )
    the goal is to split the data into multiple column ( ID, a, b, c, d )
    how ever the data in column B is sometime not ( a b c d), but repeat data ( a b c d a b c d a b c d)
    so they need to split into multiple column and multiple rows based on the ID.

    Attached is the example file, i hope i get a hint about the correct vba code.

    Thank you all.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: Split data into multiple columns and rows based on specific string

    Try next code
    
    Option Explicit
    
    Sub Treat()
    Const FR = 2                 '  Row where to start the display
    Const WkCol = 5              '  Colun where to start the display
    Const ColLst = "a,b,c,d"     '  List of items
    Dim I   As Integer, LR As Integer, II As Integer, J As Integer
    Dim ColArr, T, TT, TT1, TT2
    
        ColArr = Split(ColLst, ",")
        LR = Cells(Rows.Count, 1).End(3).Row
        II = FR
        Cells(1, WkCol).Resize(1, UBound(ColArr) + 2).EntireColumn.ClearContents
        Cells(1, WkCol) = "Id"
        Cells(1, WkCol + 1).Resize(1, UBound(ColArr) + 1) = ColArr
        For I = FR To LR
            If (Cells(I, 1) <> "") Then
                T = Split(Cells(I, 2), " ")
                For Each TT In T
                    TT1 = Split(TT, ":")(0)
                    TT2 = Split(TT, ":")(1)
                    J = Application.WorksheetFunction.Match(TT1, ColArr, 0)
                    Cells(II, WkCol) = Cells(I, 1)
                    Cells(II, WkCol).Offset(0, J) = TT2
                    If (J = UBound(ColArr) + 1) Then II = II + 1
                Next TT
            End If
        Next I
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 11-26-2018 at 05:19 AM.
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Split data into multiple columns and rows based on specific string

    Sorry for late respond, just logged back in.

    I have checked the code, it works perfectly, many thanks!
    Trying to play around.
    What if i want to add more data: example: a,b,c,d,e,f instead of a,b,c,d

    Thank you.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: Split data into multiple columns and rows based on specific string

    What if i want to add more data: example: a,b,c,d,e,f instead of a,b,c,d
    If the frame is exactly the same ( as it is in your example) you just need to complete the list of items
    Use next code
    Const ColLst = "a,b,c,d,e,f"     '  List of items

  5. #5
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Split data into multiple columns and rows based on specific string

    Thank you!

    anyway, is the code only work single word data ( non space ) , i tried to change the data into sentence, somehow not work
    example:

    a:this is text b:3 c:this is text2 d:text3 text4

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: Split data into multiple columns and rows based on specific string

    Did you put a comma between words
    Attach a new Excel sample

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Split Single Row into multiple rows based on column data
    By DoodlesMama in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-29-2017, 01:57 PM
  2. Split Column into Multiple Columns Based Off Specific Criteria
    By hellzone in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2017, 09:49 PM
  3. [SOLVED] Excel - Compare data based upon specific criteria split into columns and delimiters
    By Funkadlic01 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-01-2017, 09:36 PM
  4. split string into multiple columns
    By bmccall17 in forum Excel General
    Replies: 8
    Last Post: 06-05-2015, 12:27 AM
  5. Split Row Into Multiple Rows based on Count of Data
    By bchilme in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2014, 05:58 PM
  6. Split data into multiple documents based on specific column
    By v_nastey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2013, 08:56 AM
  7. Row split in to multiple rows based on cell data
    By bosuck in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-08-2011, 05:30 PM

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