Page 1 of 1

[VBA] กับการใช้ Custom sort

Posted: Fri May 03, 2013 10:04 am
by poipoi
ผมมีปัญหาอยู่ว่า; ต้องการให้ตัวรายงานทำงานใช้ function Custom sort โดยที่ คลุ้มทั้งหมดที่มีข้อมูลแต่ไม่เอา 2 บรรทัดสุดท้ายครับ

แล้วทำการ sort โดยให้ไปเลือกที่ column A ( CODE) sort by A - Z

ผมจึงได้ทำงานลอง Record Macro แล้วนำมาปรับแต่งนิดหน่อย ซึ่งจะต้องนำไปใช้กับ report หลายๆตัวซึ่งไม่สามารถระบุค่าสุดท้ายของใน sheet นั้นๆได้ว่าจะมีเท่าไหร่ ผมจึงได้สร้าง lastrow ขึ้นมา

แต่ทีนี้พอกดรันแล้วมันติดตรง .Apply ที่อยู่ข้างบน End With ครับ

ผมจึงใคร่อยากจะขอคำแนะนำจากอาจารย์และเพื่อนในบอร์ดแห่งนี้ครับ

ปล. ผมได้แนบ code และไฟล์ไว้แล้วครับ

Code: Select all

Sub list()
Dim lastrow As Integer
    lastrow = Range("A1048576").End(xlUp).Offset(-2, 0).Row
    
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown).Offset(-2, 0)).Select
    ActiveWorkbook.Worksheets("Sorted by Cust Code").Sort.SortFields.Clear
      ActiveWorkbook.Worksheets("Sorted by Cust Code").Sort.SortFields.Add Key:= _
        Range("A2:AA" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
   
   With ActiveWorkbook.Worksheets("Sorted by Cust Code").Sort
        .SetRange Range("A1:AA" & lastrow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Re: [VBA] กับการใช้ Custom sort

Posted: Fri May 03, 2013 1:25 pm
by tupthai
ลองทดสอบดูครับ

Code: Select all

Sub list()
      Dim lastrow As Integer
      lastrow = Cells(Rows.Count, 1).End(xlUp).Offset(-2, 0).Row
      Range("A2:AA" & lastrow).Sort Columns(1), xlAscending
End Sub

Re: [VBA] กับการใช้ Custom sort

Posted: Fri May 03, 2013 3:39 pm
by poipoi
ขอบคุณมากครับใช้งานได้ครับ

xlAscending นี่หมายถึง A-Z ใช่ไหมครับ ?

Re: [VBA] กับการใช้ Custom sort

Posted: Sun May 05, 2013 4:47 pm
by tupthai
poipoi wrote:xlAscending นี่หมายถึง A-Z ใช่ไหมครับ ?
ถูกต้องครับ