+ Reply to Thread
Results 1 to 9 of 9

Reorder elements a cell using arrays, split & join

Hybrid View

  1. #1
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Reorder elements a cell using arrays, split & join

    In Excel, I work with lists that look like this:

    Smith: My Homeland, Collected Poems
    Smith: Dr. Cuteroff, Collected Poems
    Smith: Snark, Collected Poems
    Chiu: Always, Anthology Of New American Literature
    Jones: Our Way To start The Day, Humble Pie
    Smith: Looking In
    Zhdenka: Interstices, Anthology Of New American Literature

    The format is generally Author: WOrk, Publication. Sometimes there is no Publication.

    I would like to be able to select the cells with these works and run a macro that changes the order to:

    Author: Publication - Work

    like this:

    Smith: Collected Poems - My Homeland
    Smith: Collected Poems - Dr. Cuteroff
    Smith: Collected Poems - Snark
    Chiu: Anthology Of New American Literature - Always
    Jones: Humble Pie - Our Way To start The Day
    Smith: Looking In
    Zhdenka: Anthology Of New American Literature - Interstices


    I have done some VBA stuff in the past, but nothing with split, join and arrays, which I think may be needed here. Any suggestions, advice or links will be greatly appreciated.

    Thanks,
    k8
    Last edited by k8_dog; 01-12-2011 at 01:59 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    re: Reorder elements a cell using arrays, split & join

    Based on the examples - perhaps something along the lines of:

    Sub Example()
        Dim v1 As Variant, vTemp As Variant
        Dim lngD1 As Long, lngD2 As Long
        Dim strTemp As String
        With Selection
            v1 = .Value
            For lngD2 = LBound(v1, 2) To UBound(v1, 2) Step 1
                For lngD1 = LBound(v1, 1) To UBound(v1, 1) Step 1
                    vTemp = Split(v1(lngD1, lngD2), ":")
                    v1(lngD1, lngD2) = vTemp(0)
                    vTemp = Split(vTemp(1), ",")
                    If UBound(vTemp) < 1 Then
                        strTemp = vTemp(0)
                    Else
                        strTemp = vTemp(1) & " -" & vTemp(0)
                    End If
                    v1(lngD1, lngD2) = v1(lngD1, lngD2) & ":" & strTemp
                Next lngD1
            Next lngD2
            .Value = v1
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    re: Reorder elements a cell using arrays, split & join

    Wow! Thanks. Works like a charm. I would not have been able to come up with this. Thank you so much.

  4. #4
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Reorder elements a cell using arrays, split & join

    I have one follow up question.

    I get an error my selected cells include one or more cells that Do Not contain ":".

    I'm trying to add some kind of "If InStr(Cell,":") Then..." but I can't get it to work.

    Any advice or suggestions would be appreciated.

    Thank you so much for your help.

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Reorder elements a cell using arrays, split & join

    I don't really understand what you want to happen where colon does not exist, assuming skip, then either:

    vTemp = Split(v1(lngD1,lngD2),":")
    If UBound(vTemp) > 0 Then
        v1(lngD1,lngD2) = vTemp(0)
        vTemp = Split(vTemp(1),",")
        'etc...
    End If
    or

    If InStr(v1(lngD1,lngD2),":") > 0 Then
        vTemp = Split(v1,":")
        v1(lngD1,lngD2) = vTemp(0)
        vTemp = Split(vTemp(1),",")
        'etc....
    End If

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Reorder elements a cell using arrays, split & join

    or everything in a oneliner:
    Sub snb()
      If InStr(ActiveCell, ":") > 0 Then ActiveCell = Split(ActiveCell, ":")(0) & ":" & Split(Split(ActiveCell, ":")(1), ",")(1) & " - " & Split(Split(ActiveCell, ":")(1), ",")(0)
    End Sub



  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Reorder elements a cell using arrays, split & join

    @snb, I can't help but laugh
    (I would make the point we're also working with the Selection rather than the ActiveCell but it's of little relevance)

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Reorder elements a cell using arrays, split & join

    @DO that should be our aim the whole year long....

    Sub snb2()
      sq = Selection
    
      For j = 1 To UBound(sq)
        If InStr(sq(j, 1), ":") > 0 And InStr(sq(j, 1), ",") > 0 Then sq(j, 1) = Split(sq(j, 1), ":")(0) & ":" & Split(Split(sq(j, 1), ":")(1), ",")(1) & " - " & Split(Split(sq(j, 1), ":")(1), ",")(0)
      Next
    
      Selection = sq
    End Sub
    Last edited by snb; 01-12-2011 at 06:33 AM.

  9. #9
    Registered User
    Join Date
    01-10-2011
    Location
    Oakland, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Reorder elements a cell using arrays, split & join

    Thank you, everybody. This has solved my problem, and given me a lot to think about (especially the suggestion "Avoid using VBA-code you do not understand.")

+ 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