Hello, I've used a macro to record a VBA code. Up to the point when the code is supposed to sort two columns, everything runs flawlessly. Please take note that I obtained this portion of the code by recording a macro for it. I want to apply this code to various worksheets.
Sub testSort()
'
' testSort Macro
'
'
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
 
 ActiveWorkbook.Worksheets("Analysis 1").Sort.SortFields.Clear
 ActiveWorkbook.Worksheets("Analysis 1").Sort.SortFields.Add2 Key:=Range( _
         "E5:E661325"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
 xlSortNormal
 With ActiveWorkbook.Worksheets("Analysis 1").Sort
         .SetRange Range("A4:F661325")
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
 End With
End Sub
I tried to run the code by sub categories I first created a macro that just select the portion that I want to sort I took that bit of my code and join it to the main code and it worked just great. I copied that portion of my code to my main code as before and recorded a macro that uses Excel's data sort method to sort the data, but it is failing.