+ Reply to Thread
Results 1 to 6 of 6

Macro to sort rows by date which appears in Column (B)

  1. #1
    Registered User
    Join Date
    06-11-2012
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    62

    Macro to sort rows by date which appears in Column (B)

    Hi all

    I don't know much about excel just attempting to make my job a little easier by not having to read 1000 lines of code a day.

    I am attempting to do this to keep track of my small business finances.

    I have a date and time in column (B) of my excel spreadsheet.

    I need the rows to sort according to time and date and keep the content of the row.

    Meaning that row (2) which is 2, 2/18/13 23:06, blue.... should stay together just be moved up or down according to its date.

    I know very little about macro and coding so any help would be appreciated.

    Thanks everyone

    PS: I am new to this forum thing, so I don't know if this will actually help me in any way.

    Note that I attached a sample file at the bottom of my post.

    Any help is greatly appreciated.

    Have a safe day.
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to sort rows by date which appears in Column (B)

    Tmc2159,

    I don't think this requires a macro, just the built-in Sort feature should suffice. I just selected the your data and went to the Data tab and clicked on Sort, checked "My data has headers" and told it sort by "Sand" (column B) ascending (oldest to newest), and it worked just fine for me. Does it do something unexpected for you?
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to sort rows by date which appears in Column (B)

    If you need it to be a macro, something like this should work for you:
    Please Login or Register  to view this content.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Macro to sort rows by date which appears in Column (B)

    Hi TMC,

    You really don't need a macro to do what you want. You DO need to put names in the cells on the 1st row. Then your data can be treated as a TABLE. Once in a table you can simply click on the data tab and sort it by column B.

    See http://www.gcflearnfree.org/excel2010/11.2 for some examples.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    06-11-2012
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Macro to sort rows by date which appears in Column (B)

    Thanks so much tigeravatar.

    One quick, question: How does your macro know to sort according to Column B.

    Would this work if I had timestamps in both A and B.

    Thank you so much for your help

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to sort rows by date which appears in Column (B)

    The macro is working on the used range of the sheet, which means from the top-left most populated cell to the bottom-right most populated cell. The .offset(, 1) tells the code to use the column 1 to the right, and the resize(, 1) tells the code to use only that column, which is how it knows to use column as the key column for sorting. And yes, it would still sort by column B even if you had timestamps in column A also.

+ 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