Consulting

Results 1 to 7 of 7

Thread: Solved: Rearrange columns

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: Rearrange columns

    Is there a way to rearrange columns? I have a spreadsheet with many columns and some of them should be grouped next to each other but are not. This makes it difficult to hunt for what you need. For instance, First Name, Middle Name and Last Name are spread out in varying places in the spreadsheet. Is ther a way to rearrange them so they can be next to one another?
    Peace of mind is found in some of the strangest places.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    are you wanting to do a vba sort or do you just need to manually move them one time?

    to manually move them just select the column letter which selects the entire column then cut them and select the entire column you wish to insert them in front of and right click and click on insert cut cells...not sure what your looking for.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Was hoping for a VBA approach.
    Peace of mind is found in some of the strangest places.

  4. #4
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Austen,[vba] 'How to move column "D" to before column "B"
    Columns("D").Cut
    Columns("B").Insert[/vba]Matt

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    from a simple search of the forum:
    http://www.vbaexpress.com/forum/show...t=sort+columns
    Still not sure what your looking for?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    As I had a few minutes spare.
    [vba]
    Sub MoveColumns()
    Dim Cols As String, Source As String, Target As String
    Cols = InputBox("Enter column letter(s) to move and target column" & vbCr _
    & "eg 'E:F,B' or 'F,B'", "Help Austen to move columns")
    If Cols = "" Then Exit Sub
    If InStr(1, Cols, ",") = 0 Then
    MsgBox "Enter comma between letters"
    Exit Sub
    End If
    Source = Split(Cols, ",")(0)
    On Error Resume Next
    'Fix semi colons
    Source = Replace(Source, ";", ":")
    Target = Split(Cols, ",")(1)
    Columns(Source).Cut
    Columns(Target).Insert
    End Sub

    [/vba]
    Last edited by mdmackillop; 05-25-2006 at 08:17 AM. Reason: Error handling added.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Great Malcomb, just what I needed. I know the order I want them to be in so a sub would do the trick. I can get it from here. Thanks a bunch. Consider this solved.
    Peace of mind is found in some of the strangest places.

Posting Permissions

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