Darrell MacDonald
New Member
- Joined
- Mar 25, 2021
- Messages
- 14
- Office Version
- 2010
- Platform
- Windows
Hello
I would like to create several macros to assign to ribbon icons, each of which is set to sort a selection by the column noted in the code for that button. For example, I would have buttons entitled A, B, C, etc. Button A would sort the selection by column A. Button B would sort the selection by column B, etc. I want the selected range to be dynamic and not set to a fixed range. When I record a macro for this, the code is as included below. Two of the lines in the code include a specific range ("A15:A24"). Can those lines be modified to just select the cells of the selection within the specific column?
Sub sortascendingbya()
'
' sortascendingbya Macro
'
'
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A15:A24" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A15:AC24")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I would like to create several macros to assign to ribbon icons, each of which is set to sort a selection by the column noted in the code for that button. For example, I would have buttons entitled A, B, C, etc. Button A would sort the selection by column A. Button B would sort the selection by column B, etc. I want the selected range to be dynamic and not set to a fixed range. When I record a macro for this, the code is as included below. Two of the lines in the code include a specific range ("A15:A24"). Can those lines be modified to just select the cells of the selection within the specific column?
Sub sortascendingbya()
'
' sortascendingbya Macro
'
'
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A15:A24" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A15:AC24")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub