+ Reply to Thread
Results 1 to 5 of 5

Sort Columns Individually

  1. #1
    Registered User
    Join Date
    09-06-2011
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2007
    Posts
    3

    Sort Columns Individually

    I'm thinking there's a simple solution to this but I keep hitting a brick wall.

    For simplicity's sake, I'll explain the first step:

    I have data (text) in column A and either True or False in column B (based on an IF statement). I need to sort column A and column B alphabetically based on the value of column B. This is easy enough, but I then need to do the same thing for columns C and D, then E and F, then G and H, etc.

    Is there a way to record a macro where I sort columns A and B the way I want them to be sorted, and then tell Excel to do the same thing to the next two columns, and the next two columns, and the next two columns......

    Any help would be GREATLY appreciated - I've been at this for the past two hours and am nowhere near sorting all the way to columns FFA and FFB.

  2. #2
    Registered User
    Join Date
    04-18-2011
    Location
    Surrey, UK
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Sort Columns Individually

    Hi esoterick,

    If you record a new macro, Excel will create cod for specific range of cells that you selected when you did the "demo". All you have to do afterwards is edit the code to make it more generic and youre away.

    Below is some demo macro that you might be able to edit.

    1) Open visual basic editor by going tools ---> Macro ---> Visual basic editor. Copy and paste the code below into a new module (Insert ---> Module)

    2) The macro assumes that the data to be sorted starts in row 4 for all columns and ends before row 100 in all columns, you might need to change this.

    3) I have assumed that your data starts in column 1 ( = "A") to be sorted and ends in column 199 (whatever that is!), and it is to be sorted in pairs as per your post.

    4) You then look up the reference cells for your selection which are the top-left-most cell and the bottom-right most cell in the pair of columns and get their address, and get the top-right-most cell address because this sets the column you are going to sort by.

    5) Your sheet needs to be called "Sheet1" and if it is not you need to replace all instances of sheet1 in the code below.

    6) I have done a sort based on the second collumn in each pair, ascending assuming their is no header row. You might need to replace the line starting with "Selection.Sort K...." etc with whatever your recorded macro gives.

    7) To run the macro close the visual basic editor and go Tools ---> Macro ---> Macros... Then pick "sortstuff" from the list and click run, and you should be in business.

    Id make sure you save the workbook before running the code, just in case is screws stuff up!

    Please Login or Register  to view this content.
    Good luck,

    P

  3. #3
    Registered User
    Join Date
    09-06-2011
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sort Columns Individually

    Thank you so much P - I really appreciate your help!

    I followed your instructions as closely as I could but I'm afraid I'm not there yet in my VB education.

    I was receiving errors having to the with Next not having a For or something like that. I don't want to my absence of skill to take up too much of your time but just in case you were feeling even more helpful today than you already have been, I've attached a sample of the spreadsheet I'm working with. :-)

    This the code generated form my "demo" macro:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Leith Ross; 09-07-2011 at 12:36 AM. Reason: Added Code Tags

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Sort Columns Individually

    Hi esoterick, I tried running Paul's code on your sample (I did change the start row to 1 and the end row to 1000) and it ran with no error and gave the results attached. I have left the macro in the sheet to see if that helps you
    Attached Files Attached Files
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  5. #5
    Registered User
    Join Date
    09-06-2011
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sort Columns Individually

    You guys are awesome. I can't thank you enough for your help. Hopefully I'll be able to do the same as I get more familiar with VB. Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1