I am doing the sorting on dynamic number of rows as my input comes from web application that sometimes have 1000 rows and sometime 500 or 200 etc.. I wrote the below macro but I am getting error Application error or Object defined error on
lng = Range("G" & .Rows.Count).End(x1Up).Row.
please suggest solution.
lng = Range("G" & .Rows.Count).End(x1Up).Row.
Code:
Sub Sort()
' Sort Macro
'
Dim lng As Long
lng = Range("G" & .Rows.Count).End(x1Up).Row
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G9:G" & lng _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("G8:N" & lng)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
please suggest solution.
Last edited by a moderator: