Hi There -
I am trying to add code to an existing Excel VBA Macro that sorts the attached file left to right. However I need the number of columns and rows to be dynamic. Basically I need you to start in cell F1 and ctrl+shift RIGHT and ctrl+Shift DOWN to select the datarange and then sort based on the selection.
I need to sort Left to Right by Largest to Smallest.
I tried recording it but this is what I got and it isn't going to work for different sheets with different numbers of columns and rows.
I just joined today to ask this so I can't post attachments my sample excel file is located here: https://www.dropbox.com/s/kgal60jx9bpae6l/SortLefttoRight.xlsx?dl=0
Thanks in advance!
I am trying to add code to an existing Excel VBA Macro that sorts the attached file left to right. However I need the number of columns and rows to be dynamic. Basically I need you to start in cell F1 and ctrl+shift RIGHT and ctrl+Shift DOWN to select the datarange and then sort based on the selection.
I need to sort Left to Right by Largest to Smallest.
I tried recording it but this is what I got and it isn't going to work for different sheets with different numbers of columns and rows.
I just joined today to ask this so I can't post attachments my sample excel file is located here: https://www.dropbox.com/s/kgal60jx9bpae6l/SortLefttoRight.xlsx?dl=0
Thanks in advance!
Code:
Sub sortlefttoright()
'
' sortlefttoright Macro
'
'
Range("F1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Add Key:=Range("F1:J1"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("sheet1").Sort
.SetRange Range("F1:J40")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End Sub