Page 1 of 1

Run Time Error 438 - Object Doesn't Support This Property Or Method

Posted: Fri Mar 10, 2023 11:27 am
by tigerwit
จากไฟล์ที่แนบ
โค๊ดจัดเรียงข้อมูลขึ้น Run Time Error 438 - Object Doesn't Support This Property Or Method เมื่อใช้กับ excel 2013 ลงมา

Code: Select all

Sub RankStudent()
Range("C2:I1500").Select
    ActiveWorkbook.Worksheets("DMC").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("DMC").Sort.SortFields.Add2 Key:=Range("D3:D1500"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("DMC").Sort.SortFields.Add2 Key:=Range("E3:E1500"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("DMC").Sort.SortFields.Add2 Key:=Range("F3:F1500"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("DMC").Sort
        .SetRange Range("C2:I1500")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("C3").Select
End Sub
เราจะแก้ไขโค๊ดให้สามารถใช้ได้ตั้งแต่ 2013 ขึ้นไปอย่างไรครับ

Re: Run Time Error 438 - Object Doesn't Support This Property Or Method

Posted: Fri Mar 10, 2023 7:12 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub RankStudent()
    With Worksheets("DMC")
        .Range("C2:I1500").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("D3:D1500"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("E3:E1500"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("F3:F1500"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With .Sort
            .SetRange .Parent.Range("C2:I1500")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("C3").Select
    End With
End Sub