Auto alphabetize individual columns

Adlcv99

New Member
Joined
Jul 24, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi. I am trying to figure out how to sort each column individually in alphabetical order if and when a new country name is added to that column. Picture attached of example data.
I have only been able to create a VBA to sort one column if a new country name is added but if will do nothing to the others just move them around based on the current sort. I cannout figure out how to get the other columns to sort as well individually when a new country name is added.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Presentation1.jpg
 
Upvote 0
.
See if this works for your needs. Untested here :


VBA Code:
Sub SortMultipleColumns()

    With Worksheets("Sheet1")
        with .cells(1, "A").currentregion
            .Cells.Sort Key1:=.Range("A1"), Order1:=xlAscending, _
                        Key2:=.Range("B1"), Order2:=xlAscending, _
                        Key3:=.Range("C1"), Order3:=xlAscending, _
                        Orientation:=xlTopToBottom, Header:=xlYes
        end with
    End With

End Sub
 
Upvote 0
Hi. Thanks but I couldn't get it to work. It would only sort the first column only after I had to make it run manually. Maybe I just am doing something wrong.
 
Upvote 0
.
I didn't want to make the macro this large but couldn't get it to work otherwise.

Paste this in a REGULAR MODULE :

VBA Code:
Option Explicit

Sub SortMultipleColumns()

Application.ScreenUpdating = False

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A50")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
    
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:B50")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
    
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("C1:C508")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
    
Application.ScreenUpdating = True
End Sub

Download link: Sort Columns.xlsm
 
Upvote 0
Greetings
Try this

VBA Code:
Sub test()
Sort Range("A1:E10") ' change A1:E10 as your request
'Call Sort(Range("A1:E10"))  ' change A1:E10 as your request
End Sub

Sub Sort(DataRng As Range)

'''''''''''''''' Note  without Header''''''''''''
Dim CLCnt As Long, RwCnt As Long
Dim SrtRng As Range

CLCnt = DataRng.Columns.Count 'Number of Columns
RwCnt = DataRng.Rows.Count    'Number of Rows

    For Cl = 1 To CLCnt 'to go through Range Columns On By on

    Set SrtRng = DataRng.Cells(1, Cl).Resize(RwCnt, 1) ' Sort Range column by column
        SrtRng.Sort Key1:=SrtRng.Cells(1, 1), Order1:=xlAscending, Header:=xlNo

    Next

End Sub
 
Last edited:
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A:C")) Is Nothing Then
      Target.EntireColumn.Sort key1:=Cells(1, Target.Column), order1:=xlAscending, Header:=xlYes
   End If
End Sub
This needs to go in the relevant sheet module and assumes you are looking at columns A,B & C.
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A:C")) Is Nothing Then
      Target.EntireColumn.Sort key1:=Cells(1, Target.Column), order1:=xlAscending, Header:=xlYes
   End If
End Sub
This needs to go in the relevant sheet module and assumes you are looking at columns A,B & C.
Thank you this work!
 
Upvote 0
.
I didn't want to make the macro this large but couldn't get it to work otherwise.

Paste this in a REGULAR MODULE :

VBA Code:
Option Explicit

Sub SortMultipleColumns()

Application.ScreenUpdating = False

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A50")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
   
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:B50")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
   
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("C1:C508")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlAscending
        .Apply
    End With
   
Application.ScreenUpdating = True
End Sub

Download link: Sort Columns.xlsm
Thank you but this required the button to run and I was looking for an auto sort when a new country was added incase the people entering never hit the button.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,695
Members
449,464
Latest member
againofsoul

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top