问题
I sent a friend a workbook with a macro that includes some sorting. When he tries it he gets:
Run-time error '438' Object doesn't support this property or method.
He clicks Debug to see the offending code and is taken to a line or two of (fairly simple) data sorting. The text that gets highlighted is:
ActiveWorkbook.Worksheets("Data Set").Sort.SortFields.Add2 Key:=Range( _
"H2:H8065"), SortOn:=x1SortOnValues, Order:=x1Descending, DataOption:= _
x1SortNormal
Those 3 lines are it, part of a larger sorting, and then copy/paste etc. Works fine for me (Office 365). He, in Chicago - I am in the UK - has maybe an older version of Windows.
Similar sorting occurs in other files I've sent him, and they work fine. Would anyone know what I could alter to get rid of this 438 thing? Thanks for any help.
回答1:
Use SortFields.Add instead of the newer version SortFields.Add2, which is not supported in older versions of Excel.
From the .Add2
documentation:
This API includes support for sorting off a SubField from data types, such as Geography or Stocks. You can also use the
Add
method if sorting by a data type is not needed.
... and you are not sorting by data type, so use .Add
.
来源:https://stackoverflow.com/questions/60889069/problem-a-friend-has-getting-error-438-all-the-time