I am trying to have a VBA macro sort a sheet based on 3 columns; A, B & C. A is Date, B is Country and C is League
I did a simple record macro, but it added a defined range and sheet name. I changed it as this will be run on a different sheet daily. The basic idea is to sort the data based on A, B & C making it ready to be copied using another macro which works fine. Up until now I have been manually doing the sorting part.
Content is from columns A to LQ, which is why I changed the recorded macro setting of Range(A1:LQ226).Select to simply A2:LQ, as the number of rows will vary daily. The first row contains headers
Here is the code generated in the macro recorder, although the sheet name was also in there originally and I removed it. It now gives a Compile error: Method or data member not found
I just need to be able to sort any sheet which is opened
Thanks in advance
I did a simple record macro, but it added a defined range and sheet name. I changed it as this will be run on a different sheet daily. The basic idea is to sort the data based on A, B & C making it ready to be copied using another macro which works fine. Up until now I have been manually doing the sorting part.
Content is from columns A to LQ, which is why I changed the recorded macro setting of Range(A1:LQ226).Select to simply A2:LQ, as the number of rows will vary daily. The first row contains headers
Here is the code generated in the macro recorder, although the sheet name was also in there originally and I removed it. It now gives a Compile error: Method or data member not found
Code:
Sub Sort_TP()
'
' Sort_TP Macro
'
'
Range("A2:LQ").Select
ActiveWorkbook.Worksheets.Sort.SortFields.Clear
ActiveWorkbook.Worksheets.Sort.SortFields.Add2 Key:= _
Range("A2:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets.Sort.SortFields.Add2 Key:= _
Range("B2:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets.Sort.SortFields.Add2 Key:= _
Range("C2:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets.Sort
.SetRange Range("A2:LQ")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I just need to be able to sort any sheet which is opened
Thanks in advance