You can use Range("F1").End(xlDown).Row to determine the last populated row dynamically, then use that row number to define both the sort key and the sort range. The following code assumes row 1 contains headers and column F is populated continuously.
Windows("AllTransactions.xlsm").Activate
Dim lastRow As Long
lastRow = Range("F1").End(xlDown).Row
ActiveWorkbook.Worksheets("AllTransactions").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("AllTransactions").Sort.SortFields.Add2 _
Key:=Range("B2:B" & lastRow), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("AllTransactions").Sort
.SetRange Range("A1:F" & lastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The key change is Key:=Range("B2:B" & lastRow). The & operator concatenates the column reference with the dynamically calculated last row. The .SetRange statement also uses lastRow, so the entire dataset is sorted rather than a fixed range.
Note: Range("F1").End(xlDown).Row can stop at the first blank cell in column F. A more reliable approach, especially if column F may contain gaps, is:
lastRow = Cells(Rows.Count, "F").End(xlUp).Row
This finds the last populated cell in column F by searching upward from the bottom of the worksheet.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin