+ Reply to Thread
Results 1 to 2 of 2

Modify VBA Code to sort on two columns

  1. #1
    Registered User
    Join Date
    06-20-2012
    Location
    Clearwater, Florida
    MS-Off Ver
    Excel 2007
    Posts
    28

    Modify VBA Code to sort on two columns

    Currently I am sorting data based on column B using. The data in column b is associated with data in column D. I want to add to the code below to sort by B and then within the sort on B sort on D.


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("a2:n10000")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Me.sort.SortFields.Clear
    Me.sort.SortFields.Add Key:=Range("b2:b10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Me.sort
    .SetRange Range("A2:n10000")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Application.EnableEvents = True
    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Modify VBA Code to sort on two columns

    Hi, jcicero57,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. [SOLVED] Timestamps for Multiple Columns. Need to Modify the Code
    By bssol in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-11-2022, 03:06 AM
  2. Modify VBA code to delete columns
    By dpkennedy24 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2014, 09:40 AM
  3. Replies: 7
    Last Post: 11-08-2012, 09:46 PM
  4. Sort 2 Columns Code Needed
    By SeaTiger in forum Excel General
    Replies: 0
    Last Post: 07-11-2010, 02:55 PM
  5. VBA code - How to sort two columns
    By Mula in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-28-2009, 08:38 AM

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