+ Reply to Thread
Results 1 to 30 of 30

splitting a long column of data into separate columns

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2008
    Posts
    56

    splitting a long column of data into separate columns

    Hello, i have a long column pair of data, each entry in its own cell:

    10/5/2008 8:30:00 AM
    10/5/2008 8:46:00 AM
    10/5/2008 9:14:00 AM
    10/5/2008 10:18:00 AM
    10/5/2008 10:42:00 AM
    11/5/2008 8:30:00 AM
    11/5/2008 8:46:00 AM
    11/5/2008 9:14:00 AM
    11/5/2008 10:18:00 AM
    11/5/2008 10:42:00 AM
    12/5/2008 8:30:00 AM
    12/5/2008 8:46:00 AM
    12/5/2008 9:14:00 AM
    12/5/2008 10:18:00 AM
    12/5/2008 10:42:00 AM
    13/5/2008 8:30:00 AM
    13/5/2008 8:46:00 AM
    13/5/2008 9:14:00 AM
    13/5/2008 10:18:00 AM
    13/5/2008 10:42:00 AM
    14/5/2008 8:30:00 AM
    14/5/2008 8:46:00 AM
    14/5/2008 9:14:00 AM
    14/5/2008 10:18:00 AM
    14/5/2008 10:42:00 AM

    how can i program a macro to 'split' this column according to date? please refer to the attached picture as an example. i know this is probably a simple question but please bear with me i'm still new to excel programming.

    thanks in advance!
    Attached Images Attached Images

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, Give this a try:-
    The resutant date formats are not quite correct may be my "country setting"
    Have a go, see what you find.
    Data in column "A". Results in column "B" on--.
    Dim Last As Integer, Oday As Integer, oMk, oFst, OLst, Dn, Ac
    Last = Range("a" & Rows.Count).End(xlUp).Row
    Ac = 2
    For Oday = 1 To Last
        If IsDate(Cells(Oday, 1).Value) Then
            oMk = Left(Cells(Oday, 1).Value, 2)
        Do While Left(Cells(Oday, 1).Value, 2) = oMk
                Dn = Dn + 1
            oFst = Split(Cells(Oday, 1).Value, " ")(0)
                OLst = Split(Cells(Oday, 1).Value, " ")(1)
                    Cells(Dn, Ac).Value = oFst
                        Cells(Dn, Ac + 1).Value = OLst
                        Oday = Oday + 1
                    Loop
        Ac = Ac + 2
          Oday = Oday - 1
            Dn = 0
     End If
    Next Oday
    Regards mick

  3. #3
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    Try this one. Be aware that everything is processed from bottom to top. Normally it would be start,stop time but in reverse it's stop, start.
    Sub split_data_of_day()
    'last row of data
    Dim lrow As Long
    'actual row that is being processed
    Dim countlrow As Long
    'datevalue
    Dim myvalue As Date
    'workbook
    Dim wb As Workbook
    'the worksheet
    Dim ws As Worksheet
    Set wb = ThisWorkbook
    'check every worksheet
    For Each ws In wb.Worksheets
        Select Case ws.Name
    'if different then ... don't do a thing
            Case "Sheet1", "Sheet2", "Sheet3"
            Case Else
    'process the individual shees
            With wb.Worksheets(ws.Name)
                 myvalue = "00:00:00"
                 lrow = .Range("A" & .Rows.Count).End(xlUp).Row
                 For countlrow = lrow To 2 Step -1
    'check if myvalue is different. if so, insert two columns
                     If myvalue <> .Range("A" & countlrow).Value Then
                         .Range("C:C").EntireColumn.Insert
                         .Range("C:C").EntireColumn.Insert
                         .Range("A" & countlrow).Resize(, 2).Copy
                         .Range("C" & .Range("C" & _
                         Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial xlPasteAll
                     Else
    'if time is same, just copy data
                         .Range("A" & countlrow).Resize(, 2).Copy
                         .Range("C" & .Range("C" & _
                         Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial xlPasteAll
                     End If
                 myvalue = .Range("A" & countlrow).Value
                 Next countlrow
    'remove column A and B
                 Application.DisplayAlerts = False
                 .Range("A:B").EntireColumn.Delete
                 Application.DisplayAlerts = True
             End With
        End Select
    Next ws
    End Sub
    Charlize

  4. #4
    Registered User
    Join Date
    05-14-2008
    Posts
    56

    charlize you never fail to amaze me

    thanks charlize and Mick G for your help, appreciate both answers, really. I tried both, and Mick G's code caused a Subscript Out Of Range error here

    OLst = Split(Cells(Oday, 1).Value, " ")(1)
    which i don't understand so i can't fix it on my own...

    charlize your code is simply amazing, you even added code to repeat the whole thing across every sheet. i tip my hat off to you, thanks so much.

    how do i exclude the first sheet from this macro? and is there a way to incorporate re-sorting the data into your code? perhaps a modification of:

    Columns("B:B").Select
        ActiveWorkbook.Worksheets("Lam").sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Lam").sort.SortFields.Add Key:=Range("B1"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Lam").sort
            .SetRange Range("B1:B20")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Last edited by michaelkwc; 05-22-2008 at 05:13 AM.

  5. #5
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    After some revision of another idea, I came up with this one. I believe it's what you desire.
    Sub split_data_of_day_V2()
    'last row of data
    Dim lrow As Long
    'actual row that is being processed
    Dim countlrow As Long
    'current column
    Dim curcolumn As Long
    'letter of column as string
    Dim vletter As String
    'datevalue
    Dim myvalue As Date
    'workbook
    Dim wb As Workbook
    'the worksheet
    Dim ws As Worksheet
    Set wb = ThisWorkbook
    'check every worksheet
    For Each ws In wb.Worksheets
        Select Case ws.Name
    'if different then ... don't do a thing
            Case "Sheet1", "Sheet2", "Sheet3"
            Case Else
    'process the individual shees
            With wb.Worksheets(ws.Name)
                 myvalue = "00:00:00"
                 curcolumn = 1
                 lrow = .Range("A" & .Rows.Count).End(xlUp).Row
                 For countlrow = 2 To lrow Step 1
    'check if myvalue is different. if so, shift up two columns
                     If myvalue <> .Range("A" & countlrow).Value Then
                         curcolumn = curcolumn + 2
    'get the columnletter to where we need to paste the info
                         vletter = Split(Cells(1, curcolumn).Address, "$")(1)
                         .Range("A" & countlrow).Resize(, 2).Copy
                         .Range(vletter & .Range(vletter & _
                            Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial xlPasteAll
                     Else
    'if time is same, just copy and paste data
                         .Range("A" & countlrow).Resize(, 2).Copy
                         .Range(vletter & .Range(vletter & _
                            Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial xlPasteAll
                     End If
                 myvalue = .Range("A" & countlrow).Value
                 Next countlrow
    'remove column A and B
                 Application.DisplayAlerts = False
                 .Range("A:B").EntireColumn.Delete
                 Application.DisplayAlerts = True
             End With
        End Select
    Next ws
    End Sub
    Charlize

  6. #6
    Registered User
    Join Date
    05-14-2008
    Posts
    56
    .Range(vletter & .Range(vletter & _
    Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial xlPasteAll
    i'm getting this error: Application-defined or Object-defined error

    but it's been a long day and my head's melting, so perhaps it's an error on my side. will try again tomorrow, update how it goes.

    thanks again charlize, don't know what i'd do without you.

    ~Michael

+ 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