+ Reply to Thread
Results 1 to 14 of 14

Need macro to split one column into multiple columns based on _ and spacing

Hybrid View

  1. #1
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Need macro to split one column into multiple columns based on _ and spacing

    Different method for Sheet1

    Perhaps few lines are not converted properly though...
    Sub test()
        Dim a, i As Long, ii As Long, b() As String, n As Long, x, temp
        With Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 4)
            a = .Value
            ReDim b(1 To UBound(a, 1), 1 To 5)
            With CreateObject("VBScript.RegExp")
                .Global = True
                On Error Resume Next
                For i = 1 To UBound(a, 1)
                    If a(i, 1) <> "" Then
                        .Pattern = "\.pdf"
                        a(i, 1) = .Replace(a(i, 1), "")
                        .Pattern = "00"
                        a(i, 1) = .Replace(a(i, 1), "0")
                        .Pattern = "(.+)(?:[_\-])(\d{1,2})(\d{1,2})(\d{4}|\d{2}) (.*)$"
                        If .test(a(i, 1)) Then
                            b(i, 4) = CDate(.Replace(a(i, 1), "$4/$2/$3"))
                            b(i, 5) = Trim$(.Replace(a(i, 1), "$5"))
                            temp = .Replace(a(i, 1), "$1")
                            .Pattern = "[^_]+"
                            For ii = 0 To .Execute(temp).Count
                                b(i, ii + 1) = .Execute(temp)(ii)
                            Next
                        Else
                            .Pattern = "(.+)(?:[_\-])(\d{1,2})(\d{1,2})(\d{2|\d{4})\d?"
                            If .test(a(i, 1)) Then
                                temp = .Replace(a(i, 1), "$4/$2/$3")
                                b(i, 4) = CDate(.Replace(a(i, 1), "$4/$2/$3"))
                                temp = .Replace(a(i, 1), "$1")
                                .Pattern = "(\d+)[_\-](\D+)[_\-](\d+)"
                                For ii = 1 To 3
                                    b(i, ii) = .Replace(temp, "$" & ii)
                                Next
                                b(i, 5) = Trim$(a(i, 2) + a(i, 3) & " " & a(i, 4))
                            End If
                        End If
                        temp = ""
                    End If
                Next
            End With
            With .Offset(, .Columns.Count + 2).Resize(, 5)
                .Value = b
                With .Columns(4)
                    .NumberFormat = "mmddyyyy"
                    .Value = .Value
                End With
            End With
        End With
    End Sub
    Last edited by jindon; 09-13-2012 at 10:24 PM.

+ 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