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?

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