+ Reply to Thread
Results 1 to 3 of 3

Need fix for VBA Code to Custom Sort multiple columns that includes DATE columns

  1. #1
    Registered User
    Join Date
    11-13-2015
    Location
    WA
    MS-Off Ver
    365 Enterprise
    Posts
    21

    Need fix for VBA Code to Custom Sort multiple columns that includes DATE columns

    I have a simple code that works, but Columns "G" and "H" contain DATES (example 01/03/22). (Column B is text range) I want G to sort oldest date to newest date, then H oldest to newest, then B alphabetical.
    I am new to VBA, so can someone help me correct this?

    Sub SortMultipleColumns()
    With ActiveSheet.Sort
    .SortFields.Add Key:=Range("G1"), Order:=xlAscending
    .SortFields.Add Key:=Range("H1"), Order:=xlAscending
    .SortFields.Add Key:=Range("B1"), Order:=xlAscending
    .SetRange Range("A1:H1000")
    .Header = xlYes
    .Apply
    End With
    End Sub

    Please see image of the problem:
    Example.JPG
    Last edited by Kirk3737; 11-14-2020 at 09:14 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Need to fix VBA Code to Auto Sort multiple columns that includes DATE columns

    ...Seems to work OK for me. Why do you think it doesn't? I'm assuming of course your dates are indeed proper date numbers and not just strings that look like dates
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-13-2015
    Location
    WA
    MS-Off Ver
    365 Enterprise
    Posts
    21

    Re: Need to fix VBA Code to Auto Sort multiple columns that includes DATE columns

    Columns G and H are formatted as Date Category, Standard Type 00/00/00. I have dates in ending 2020, 2021, 2022. When macro is run, it seems to sort by column B first. Here is an example:
    Attachment 704142

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Automatically sort by date, multiple columns, and blanks
    By shaun.burke in forum Excel General
    Replies: 1
    Last Post: 08-16-2019, 10:44 AM
  2. Count which includes multiple columns including multiple criteria
    By Zou in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-27-2017, 10:43 AM
  3. VBA Code To Auto Sort Z-A In Certain Columns
    By npsnps in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-10-2016, 04:58 PM
  4. Replies: 2
    Last Post: 06-30-2015, 04:45 PM
  5. Auto Sorting Multiple Columns - Last Row Will Not Sort
    By RedJeep03 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2013, 02:40 AM
  6. Auto sort code to include adjacent columns
    By Frantelle in forum Excel General
    Replies: 3
    Last Post: 11-17-2012, 11:47 AM
  7. Replies: 4
    Last Post: 03-22-2009, 03:30 PM

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