Consulting

Results 1 to 5 of 5

Thread: rearranging columns

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Posts
    32
    Location

    rearranging columns

    Hello,

    I am trying to write a macro that will rearrange columns in a certain order. Each column's 1st cell has a name as follows. Column A = Eqp Additional Info Column B = Equipment Name Column C = Time Up Column D = Alarm Type.

    I would like the columns to be arranged as Column A = Alarm Type Column B = Time Up Column C = Equipment Name Column D = Eqp Additional Info.

    I know I can just manually move columns...but each user's sheet could have thse columns in a different order as shown above. The only common point is that the column names are the same. I was trying to focus on that using maybe an array or some way to look for those column names and then rearrange.


    Thanks

    lhardee

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This might get you started in the right direction. If you wanted to move columns d, e & f to position b you would use D:F,b in the messagebox.


    If you want to move column d to position b you would use d,b in the messagebox. The comma seperator is the key.

    could probably be expanded on if it seems to be useful.

    [VBA]Option Explicit
    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'")
    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]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Feb 2008
    Posts
    32
    Location
    Thanks lucas,


    Your code does the trick but I am looking for a more automated way where the user is not asked what columns to rearrange. I will work with your code to see what I can come up with.

    Thanks for the assistance.

    lhardee

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you need more help, post back here. This thread exemplifies the value of interoffice standards by the way.......
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Would Excel's native Sort (Option: left to right) on a custom list work for you?

Posting Permissions

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