Results 1 to 10 of 14

Thread: Swapping (Rearranging) Multiple Columns of Data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    12
    A sorting alternative:
    Code:
    Sub snb()
      sn = Split("C,F,A,G,B,J,E,D,I,H", ",")
      For j = 0 To UBound(sn)
        sn(j) = Columns(sn(j)).Column
      Next
    
      Cells(1).CurrentRegion.Rows(1).Offset(Cells(1).CurrentRegion.Rows.Count) = sn
      With Cells(1).CurrentRegion
        .Sort Cells(.Rows.Count, 1), , , , , , , , , , xlSortRows
        .Rows(.Rows.Count).ClearContents
      End With
    End Sub
    or if the number of columns doesn't exceed the alphabet:

    Code:
    Sub snb_002()
      Cells(1).CurrentRegion.Offset(10) = Application.Index(Cells(1).CurrentRegion, Evaluate("Row(" & Cells(1).CurrentRegion.Address & ")"), [transpose(code(mid("CFAGBJEDIH",row(1:10),1))-64)])
    end sub
    Last edited by snb; 08-11-2012 at 06:44 PM.

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by Rick Rothstein View Post
    Code:
    Sub RearrangeColumns()
      Dim X As Long, LastRow As Long, Letters As Variant
      Const NewOrder As String ="C,B,E,F,H,AC,K,AF,T,M,S,G,X,I,Z,AA,L,AG,AE,AH,AD,AI,A,D,J,N,O,P,Q,R,U,V,W,Y,AB,AJ"
      LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
      Letters = Split(NewOrder, ",")
       For X = 0 To UBound(Letters)
        Letters(X) = Columns(Letters(X)).Column
      Next
      Range("A1").Resize(LastRow, UBound(Letters)) = Application.Index(Cells, Evaluate("ROW(1:" & LastRow & ")"), Letters)
    End Sub
    Quote Originally Posted by snb View Post
    Code:
    Sub snb()
      sn = Split("C,F,A,G,B,J,E,D,C,H", ",")
      For j = 0 To UBound(sn)
        sn(j) = Columns(sn(j)).Column
      Next
    
      Cells(1).CurrentRegion.Rows(1).Offset(Cells(1).CurrentRegion.Rows.Count) = sn
      With Cells(1).CurrentRegion
        .Sort Cells(.Rows.Count, 1), , , , , , , , , , xlSortRows
        .Rows(.Rows.Count).ClearContents
      End With
    End Sub
    While I haven't tried it out, your code looks like it should work, but personally, I think your "sort order string" would be harder to set up than mine. With mine, you just look at the columns in the order you want them to be ("I want C first, then B, then E, etc.") and then write the column letters down in that order... easy to do by sight. With your method, I would have to know where the column will end up at after the sort so that I can write those column letters down in that order instead ("A will be C in the new order, B will be F in the new order, C will be A in the new order, etc.)... which, to me, does not seem as easy to do. However, you have offered a valid alternative approach to the problem, so I thank you for that.

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2013, 07:34 AM
  2. Converge Data From Multiple Columns To Single Column
    By ayazgreat in forum Excel Help
    Replies: 3
    Last Post: 12-14-2012, 10:55 PM
  3. Swapping columns Error 2
    By jomili in forum Excel Help
    Replies: 1
    Last Post: 11-16-2012, 08:52 PM
  4. Swapping Multiple Columns of Data
    By jomili in forum Excel Help
    Replies: 5
    Last Post: 10-01-2012, 05:56 PM
  5. Replies: 2
    Last Post: 06-14-2012, 04:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •