Consulting

Results 1 to 4 of 4

Thread: Solved: Sort Non-Adjacent Columns

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Solved: Sort Non-Adjacent Columns

    Can a sort be done on non-adjacent columns? I have column A and column AE that I need sorted by key1=AE and key2=A, can this be done?

    Thanks
    Gary

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Gary,

    Are you saying that you want to sort only those columns, leaving B:AD as they are?
    You can do that by moving them next to each other beforehand:[vba]Sub dlfkjdf()
    Application.ScreenUpdating = True
    With ActiveSheet
    .Columns("AE").Cut
    .Columns("B").Insert
    .Range("A:B").Sort Key1:=.Columns("B"), Key2:=.Columns("A"), Header:=xlYes
    .Columns("B").Cut
    .Columns("AF").Insert
    End With
    Application.ScreenUpdating = True
    End Sub[/vba]Matt

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Matt that is exactly what i am saying. I was going to just move column AE to Column A but then I would have to hide it which would cause havoc on my code. (Don't know how to account for for hidden columns in my code). I am a novice Excel and VBA user. I will give your code a try.

    Thanks
    Gary

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Matt this is what I ended up with within my sorting sub and so far so good.


    [VBA]
    'Move Column AF to Column B to Sort, Then Move It Back To Its Original Place
    With ActiveSheet
    .Columns("AF").Cut
    .Columns("B").Insert
    .Range("A5:B" & LastRow).Sort Key1:=Range("B5"), Key2:=Range("A5"), Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    .Columns("B").Cut
    .Columns("AG").Insert
    End With
    [/VBA]

    Thanks Again
    Gary

Posting Permissions

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