Consulting

Results 1 to 9 of 9

Thread: flips left-to-right sequence of selected columns

  1. #1

    flips left-to-right sequence of selected columns

    Hi,

    I know this must be some ready answers on the web but I had been searching the whole evening without success for the right code. Hope that someone could please help.

    Let's say, I select a contiguous entire range of columns (example, from H to S, 12 columns representing 12 months of data) and I want to flip all the selected columns from left to right. If it is from Jan, Feb... to Dec, I want it to re-arrange to the sequence Dec,Nov,Oct... to Jan.

    Thanks a lot.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I don't know about 2003 or earlier but in 2007 you can highlight data choose DATA, SORT, OPTIONS choose SORT LEFT TO RIGHT, then in the ORDER dropdown choose custom and create a new custom list then sort!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Thanks Simon. I will read the cross-post message very carefully and make sure I do it right always.

  4. #4
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Hi Alienscript,

    In order for this to work correctly the values for the month in each cell need to be input as - 01/2008, 02/2008, etc then format cells as custom - mmm

    By recording a macro the code below shows how to sort the columns in the opposite order;

    [vba]
    Sub opporder ()
    Columns("A:L").Select '//change range to suit your requirements
    Selection.Sort Key1:=range("A1"), Order1:=xlDescending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
    DataOption1:=xlSortNormal
    range("A1").Select

    End Sub
    [/vba]

    Marshybid
    Last edited by marshybid; 07-08-2008 at 03:42 AM.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    ActiveSheet.DisplayRightToLeft = True
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by xld
    [vba]

    ActiveSheet.DisplayRightToLeft = True
    [/vba]
    Hi xld,

    That's a clever way of doing it

    I guess this would just flip every column in the active sheet? This way you would not need to worry about the cell values or formatting?? is that right? You always make it look way too easy for beginners like me

    I opted for the record a macro method to teach myself how it could be done

    Marshybid

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It was just a bit of fun really, I am sure it affecst the spreadsheet in ways the OP doesn't want, but interesting anyway.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This works on any Selection that has any (non-duplicates) text in the top row
    [VBA]Sub Macro1()
    Dim customListNum As Long
    With Selection
    Application.AddCustomList ListArray:=Application.Index(.Cells, 1, 0)
    customListNum = Application.GetCustomListNum(Application.Index(.Cells, 1, 0).Value)

    .Sort Key1:=.Range("a1"), Order1:=xlDescending, Header:=xlNo, _
    OrderCustom:=customListNum + 1, MatchCase:=False, Orientation:=xlLeftToRight
    Application.DeleteCustomList ListNum:=customListNum
    End With
    End Sub[/VBA]

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can always add a numbered helper row and sort using that.
    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'

Posting Permissions

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