Sort all columns individually

  • Hi,

    I have got a sheet, in which I have got a dynamic number of columns with a number of dynamic rows.

    It is a database export where I have got table names (headers) in each column and among them there are dynamic numbers of strings (attributes) which are not alphabetically sorted yet. I don't know how exactly to sort them with a vba script. I need this, because I want all columns as named ranges for dynamic dropdown lists.

    I have seen a few possible solutions but I don't want to select manually ranges. This should work without any user interaction - just by calling this sub in another script.

    I appreciate your suggestions.

  • This seems to work, but how could I modify this without pre selecting the ranges but instead just select all content without headers?

    Sorry, I did not find a button for editing my post.

  • Does this do it?

    Sub Sort_Indiv_Columns()
    Dim i As Long, lr As Long
    With ActiveSheet
    For i = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
    lr = .Cells(.Rows.Count, i).End(xlUp).Row
    .Range(.Cells(1, i), .Cells(lr, i)).Sort Key1:=.Cells(1, i), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Next i
    End With
    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!