Excel VBA VBA Runtime Error

I use VBA for Excel 2010 and accidentally get the following error:

Runtime error "1004": "The sort link is not valid. Make sure it is in the data you want to sort, and the first sort field does not match or is empty."

This is the code

'Sort the active rows With ActiveWorkbook.Worksheets("Product Backlog").Sort .SetRange Range("A4:F51") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 
+6
source share
1 answer

Sort field is empty, this is your problem. I have never used a Sort object like yours, but I see that you have not defined a key or range for sorting, but only a range that needs to be sorted. A key must be defined, for example Range ("A4") or something else. I looked at it, it should have .sortfields.add (range), for example:

 'Sort the active rows With ActiveWorkbook.Worksheets("Product Backlog").Sort .SetRange Range("A4:F51") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .SortFields.Add Key:=Range("A4:F51").Columns(1), SortOn:=xlSortOnValues, _ Order:=xlDescending, DataOption:=xlSortNormal .Apply End With 

I use the sort function as follows:

 ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Sort _ Key1:= ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Columns(1), _ Header:= xlYes, _ Orientation:=xlSortColumns, _ MatchCase:=False, _ SortMethod:=xlPinYin 
+4
source

Source: https://habr.com/ru/post/904766/


All Articles