Alphabetical Order Macro [VBA/Excel]

Solved
styvea Posted messages 36 Status Member -  
styvea Posted messages 36 Status Member -
Hello,

I would like to know if there is a macro in Excel that can sort a table alphabetically based on a specific column, please.

I tried using "record a macro" and doing it with the mouse, but I don't see what to change to get what I want.

Here is the code I got:

Sub ordre_alpha() ' ' ordre_alpha Macro ' ' Range("A1:K18").Select ActiveWorkbook.Worksheets("Mars").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Mars").Sort.SortFields.Add Key:=Range("B2:B18"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Mars").Sort .SetRange Range("A1:K18") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub


Mars being the name of the current sheet, but since the sheet can change at any time, I would need something like ActiveSheets. And since the size of the table is variable, I need a Range that selects the entire table on the page.

I tried this code:

Sub Macro3() ' ' Macro3 Macro ' ' nbc = UsedRange.Columns.Count nbl = UsedRange.Rows.Count UsedRange.Columns.Count.Select ActiveWorkbook.ActiveSheet.Sort.sortfiels.Clear ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("B2:nbl"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets.ActiveSheet.Sort .SetRange Range("nbl:nbc") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub 

But it doesn't work and I can't really see where the error is...

Thanks to anyone who can help :) Best regards

StyveA

Configuration: Windows 7 / Internet Explorer 8.0

1 answer

g Posted messages 1285 Status Member 578
 
Hello,

Sub TriAlpha()
Range("A1:Z1000").Sort Key1:=Range("B1"), Order1:=xlAscending
End Sub

This macro sorts alphabetically by column B in the range A1:Z1000 (it can of course be adjusted).

Have a nice day.
17
styvea Posted messages 36 Status Member 2
 
Oh, that's short compared to what I wrote, lol...

Thank you very much, I'll try that right away!
(I suppose it'll work on the current sheet?
0