1

I have this macro that works on my office computer but when I run it at a client's computer It doesn't work and shows the

Error 438 object doesn't support this property

marking this line:

 ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas)

(My excel version is 365 and the client's is 2013)

I've tried changing Worksheets by Sheets, ActiveWorkbook by Sheet but nothing worked.

Columns("E:G").Select
ActiveSheet.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), _
    Header:=xlYes
Cells.Select
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas) _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Hoja1").Sort
    .SetRange Range("A1:X" & nFilas)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Brian Smith
  • 11
  • 2
  • 5
  • what does nFilas equate to when the code is run? – S Meaden Aug 08 '19 at 14:48
  • 4
    Change `.Add2` into `.Add` I think `.Add2` was introduced in a later Excel version. • Also you might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). – Pᴇʜ Aug 08 '19 at 14:48
  • nFilas is the lenght of the rows in column. – Brian Smith Aug 08 '19 at 15:53

1 Answers1

0

Change .Add2 into .Add I think .Add2 was introduced in a later Excel version.

I recommend the following improvement:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Hoja1")

ws.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
With ws.Sort
    .SortFields.Clear
    .SortFields.Add Key:=ws.Range("E2:E" & nFilas), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    .SetRange ws.Range("A1:X" & nFilas)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73