VBA CODE sorting any range of data

kanelones

Board Regular
Joined
Aug 25, 2010
Messages
103
Hi Guys, can you help me writing a VBA code line.

Need to execute a macro, where gives me the option to sort any range of data to my choice , after i select it ,instead pressing alt+h+v,etc, etc..

Maybe this code, could ask me from witch column i want to sort the range, or I it run it for a fixed column, I have certain data ranges , that always need to be sorted by the same column.

Can it be done ?

Thanks!
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I recorded a macro and added a few variables, and made the data range general
- assumes that there is nothing in row above the headers

Adapting:
To learn what you need to do, record a few macros when you select sort parameters manually and learn what changes need to be made
- adapt it to give you the range of choices you need

To test:
- Put code in a standard module
- Select any cell in your data range
- Run the macro
- It sorts on that column


Code:
Sub SortData()
    
    Dim cel As Range: Set cel = Selection.Cells(1)
    Dim rng As Range: Set rng = cel.CurrentRegion
    
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add2 Key:=cel, _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange rng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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