+ Reply to Thread
Results 1 to 9 of 9

Macro to split data into multiple cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Macro to split data into multiple cells

    Hi All,

    I need some help on a macro.
    I have multiple entries in column C which I would like to split it up and show in multiple cells based on Column A& Column B. For example as below

    Original
    Column A Column B Column C
    A 13.05.07 1,2,4,5
    B 15.08.09 9,8,7,6


    Output
    Column A Column B Column C
    A 13.05.07 1
    A 13.05.07 2
    A 13.05.07 4
    A 13.05.07 5

    Any idea anyone able to help me on this? Have attached the sample in tab Sample and how it should be in tab Output.
    Hope someone is able to help me out. :-)

    Kind Regards,
    Mark.
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,033

    Re: Macro to split data into multiple cells

    Try this:

    Option Explicit
    
    Sub mark888()
        Application.ScreenUpdating = False
        Dim i As Long, lr As Long, lc As Long, j As Long
        Dim lr2 As Long, s1 As Worksheet, s2 As Worksheet
        Set s1 = Sheets("Sample")
        Set s2 = Sheets("Output")
        lr = s1.Range("A" & Rows.Count).End(xlUp).Row
        s1.Range("A1:C1").Copy s2.Range("A1")
        With s1
            .Range("C2:C" & lr).TextToColumns Destination:=Range("C2"), _
                                              DataType:=xlDelimited, _
                                              TextQualifier:=xlDoubleQuote, _
                                              Semicolon:=True
            For i = 2 To lr
                lc = .Cells(i, Columns.Count).End(xlToLeft).Column
                lr2 = s2.Range("C" & Rows.Count).End(xlUp).Row
                .Range("A" & i & ":B" & i).Copy s2.Range("A" & lr2 + 1)
                .Range(.Cells(i, 3), .Cells(i, lc)).Copy
                s2.Range("C" & lr2 + 1).PasteSpecial xlPasteAll, , , True
            Next i
            Application.CutCopyMode = False
        End With
    
        With s2
            lr2 = .Range("C" & Rows.Count).End(xlUp).Row
            For i = 3 To lr2
                If .Range("A" & i) = "" Then
                    .Range("A" & i) = .Range("A" & i - 1)
                    .Range("B" & i) = .Range("B" & i - 1)
                End If
            Next i
        End With
        Application.ScreenUpdating = True
        MsgBox "Task Completed"
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Re: Macro to split data into multiple cells

    Quote Originally Posted by alansidman View Post
    Try this:

    Option Explicit
    
    Sub mark888()
        Application.ScreenUpdating = False
        Dim i As Long, lr As Long, lc As Long, j As Long
        Dim lr2 As Long, s1 As Worksheet, s2 As Worksheet
        Set s1 = Sheets("Sample")
        Set s2 = Sheets("Output")
        lr = s1.Range("A" & Rows.Count).End(xlUp).Row
        s1.Range("A1:C1").Copy s2.Range("A1")
        With s1
            .Range("C2:C" & lr).TextToColumns Destination:=Range("C2"), _
                                              DataType:=xlDelimited, _
                                              TextQualifier:=xlDoubleQuote, _
                                              Semicolon:=True
            For i = 2 To lr
                lc = .Cells(i, Columns.Count).End(xlToLeft).Column
                lr2 = s2.Range("C" & Rows.Count).End(xlUp).Row
                .Range("A" & i & ":B" & i).Copy s2.Range("A" & lr2 + 1)
                .Range(.Cells(i, 3), .Cells(i, lc)).Copy
                s2.Range("C" & lr2 + 1).PasteSpecial xlPasteAll, , , True
            Next i
            Application.CutCopyMode = False
        End With
    
        With s2
            lr2 = .Range("C" & Rows.Count).End(xlUp).Row
            For i = 3 To lr2
                If .Range("A" & i) = "" Then
                    .Range("A" & i) = .Range("A" & i - 1)
                    .Range("B" & i) = .Range("B" & i - 1)
                End If
            Next i
        End With
        Application.ScreenUpdating = True
        MsgBox "Task Completed"
    End Sub
    Hi Alan,

    This works althought there seems to be some mismatch. For example the first record shows 40 in tab Sample but 43 in tab Output.
    Manually i calculated it should show only 40 records.

    Kind Regards,
    MArk.

  4. #4
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Re: Macro to split data into multiple cells

    Quote Originally Posted by mark888 View Post
    Hi Alan,

    This works althought there seems to be some mismatch. For example the first record shows 40 in tab Sample but 43 in tab Output.
    Manually i calculated it should show only 40 records.

    Kind Regards,
    MArk.
    Hi Alan,
    Sorry i know what went wrong. It calculated the first 3 which I already had there in the first place. :-)

    Kind Regards,
    Mark.

  5. #5
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Re: Macro to split data into multiple cells

    Quote Originally Posted by mark888 View Post
    Hi Alan,

    This works althought there seems to be some mismatch. For example the first record shows 40 in tab Sample but 43 in tab Output.
    Manually i calculated it should show only 40 records.

    Kind Regards,
    MArk.
    Hi Alan,
    Sorry i know what went wrong. It calculated the first 3 which I already had there in the first place. :-)

    Kind Regards,
    Mark.

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro to split data into multiple cells

    Try:
    Sub a()
        Dim i As Long, j As Long, v, ws As Worksheet
        Set ws = ActiveSheet
        With Sheets.Add
            .Cells(1, 1).Resize(, 3).Value = ws.Cells(1, 1).Resize(, 3).Value
            For i = 2 To ws.Cells(rows.count, 3).End(xlUp).row
                v = Split(ws.Cells(i, 3).Value, ";")
                For j = LBound(v) To UBound(v)
                    .Cells(rows.count, 1).End(xlUp).Offset(1).Resize(, 3).Value = Array(ws.Cells(i, 1).Value, ws.Cells(i, 2).Value, v(j))
                Next
            Next
        End With
    End Sub
    多么想要告诉你 我好喜欢你

  7. #7
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Re: Macro to split data into multiple cells

    Quote Originally Posted by millz View Post
    Try:
    Sub a()
        Dim i As Long, j As Long, v, ws As Worksheet
        Set ws = ActiveSheet
        With Sheets.Add
            .Cells(1, 1).Resize(, 3).Value = ws.Cells(1, 1).Resize(, 3).Value
            For i = 2 To ws.Cells(rows.count, 3).End(xlUp).row
                v = Split(ws.Cells(i, 3).Value, ";")
                For j = LBound(v) To UBound(v)
                    .Cells(rows.count, 1).End(xlUp).Offset(1).Resize(, 3).Value = Array(ws.Cells(i, 1).Value, ws.Cells(i, 2).Value, v(j))
                Next
            Next
        End With
    End Sub
    Hello Millz,

    This works perfectly as well. Many thanks sir.

    Kind Regards,
    MArk.

  8. #8
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Macro to split data into multiple cells

    try this:

    Sub zz()
    Dim ar As Variant, br(1 To 1048576, 1 To 3), i&, j&, k&, n%, t
    ar = Sheets(1).[a1].CurrentRegion
    For k = 2 To UBound(ar)
        t = Split(ar(k, 3), ";")
        For i = 0 To UBound(t)
           n = n + 1
           br(n, 1) = ar(k, 1)
           br(n, 2) = ar(k, 2)
           br(n, 3) = t(i)
        Next
    Next
    With Sheets(2)
        .Range("a2:c1048576").Clear
        .Range("a" & .[a1048576].End(3).Row + 1).Resize(n, 3) = br
    End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    01-02-2015
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    2007
    Posts
    272

    Re: Macro to split data into multiple cells

    Quote Originally Posted by ikboy View Post
    try this:

    Sub zz()
    Dim ar As Variant, br(1 To 1048576, 1 To 3), i&, j&, k&, n%, t
    ar = Sheets(1).[a1].CurrentRegion
    For k = 2 To UBound(ar)
        t = Split(ar(k, 3), ";")
        For i = 0 To UBound(t)
           n = n + 1
           br(n, 1) = ar(k, 1)
           br(n, 2) = ar(k, 2)
           br(n, 3) = t(i)
        Next
    Next
    With Sheets(2)
        .Range("a2:c1048576").Clear
        .Range("a" & .[a1048576].End(3).Row + 1).Resize(n, 3) = br
    End With
    End Sub
    Hi Ikboy,

    This works perfectly as well. Many thanks all. :-)

    Kind Regards,
    Mark.

+ 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. MACRO to split new data into multiple tabs but not overwrite existing data
    By amo899115 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-11-2016, 01:42 PM
  2. [SOLVED] Macro attempting to split cells containing multiple co-ordinates into appropriate columns
    By Ethanrholt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-06-2014, 10:56 AM
  3. Macro to split 1 row of data into multiple
    By ks04 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2013, 03:27 AM
  4. [SOLVED] Macro to split cell data across multiple cells based on format-
    By Biased Historian in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-30-2013, 12:57 PM
  5. Macro to split Cells delimter comma to multiple Rows.
    By pavan5183 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-17-2013, 10:24 AM
  6. [SOLVED] How do you split a cell that contains multiple data to the cells beside it?
    By kjy1989 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2013, 08:37 AM
  7. Split cells long text with Max 40 character to multiple row using Macro
    By chee1012 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-31-2012, 10:10 AM

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