+ Reply to Thread
Results 1 to 5 of 5

Macro to Sort column B and C alphabetically

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Australia
    MS-Off Ver
    2010, 2013
    Posts
    39

    Macro to Sort column B and C alphabetically

    Hello i have a members list with with cell be containing members name (starting in B3) in the following format (lastname, firstname) then in Column C i have the members location. i have made a VBA that sorts the lists but it doesnt keep the members location and name together!
    i want it to auto soft the names alphabetically but keep their location beside it in column C
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x As Range
    Set x = Cells(2, Target.Column)
    Dim y As Range
    Set y = Cells(1000, Target.Column)
    
    If Target.Column = 1 Or Target.Column = 2 Or Target.Column = 3 Then
    Range(x, y).Sort Key1:=Target, Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End If
    End Sub
    this is the code i am using, Can anyone tell me where i have gone wrong?
    regards
    Anthony

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,754

    Re: Macro to Sort column B and C alphabetically

    Your range(x,y).sort only takes into account the target column. Your range needs to be expanded to include the non-target ranges.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-25-2014
    Location
    Australia
    MS-Off Ver
    2010, 2013
    Posts
    39

    Re: Macro to Sort column B and C alphabetically

    Hi Alansidman,
    i understand what you mean but unsure how to put it into code! sorry im new to VBA

  4. #4
    Registered User
    Join Date
    09-25-2014
    Location
    Australia
    MS-Off Ver
    2010, 2013
    Posts
    39

    Re: Macro to Sort column B and C alphabetically

    i recorded this one and it works as a button but when i put it into the worksheet code it does not auto work.
    Sub Autosort()
    '
    ' Autosort Macro
    '
    
    '
        Range("B3:C1300").Select
        ActiveWorkbook.Worksheets("Members").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Members").Sort.SortFields.Add Key:=Range("B3"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Members").Sort
            .SetRange Range("B3:C1000")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWindow.SmallScroll Down:=203
    End Sub

  5. #5
    Registered User
    Join Date
    09-25-2014
    Location
    Australia
    MS-Off Ver
    2010, 2013
    Posts
    39

    Re: Macro to Sort column B and C alphabetically

    this code works however its not automatically doing it if i make a button it works
    Sub Autosort()
    '
    ' Autosort Macro
    '
    
    '
        Range("B3:C1300").Select
        ActiveWorkbook.Worksheets("Members").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Members").Sort.SortFields.Add Key:=Range("B3"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Members").Sort
            .SetRange Range("B3:C1000")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWindow.SmallScroll Down:=203
    End Sub

+ 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. Auto sort alphabetically using basic macro
    By Tyler1226 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-20-2012, 04:18 PM
  2. Sort Data Alphabetically without using a Macro
    By Brkenarrow in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 09-14-2012, 09:54 AM
  3. [SOLVED] Macro to list data from a table and sort it alphabetically
    By Siglen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-16-2012, 11:06 AM
  4. excel to sort data in a column alphabetically
    By zachmcnulty in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-02-2008, 12:52 PM
  5. Macro to sort worksheets alphabetically
    By Re: Inserting an option button in Word in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2005, 10:05 AM

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