Sort by row based on column value
Hello
I'm using Excel 2010 and I would like to sort by row based on values in a column.
So far I have
Code:
Sub sortRow()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim ViewMode As Long
Dim i As Long
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row + 1
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Firstrow To Lastrow
With sht.Sort
.SortFields.Clear
.SortFields.Add Key:=rng, SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
.SetRange rng
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Next Lrow
End With
End Sub
I believe the problem is at the .SetRange rng not sure what to put if I want column B for example
I tried .setrange :=(B:B) but it errors
I should mention I wish to order the row in descending order based on the column word