Page 1 of 1

โค้ด filterRows

Posted: Sat Apr 11, 2026 6:32 pm
by วังวู ช่ง
สวัสดีทุกท่าน ช่วยตรวจสอบโค้ดนี้หน่อยครับ มันทำงานช้ามาก

Code: Select all

Sub FilterRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim targetValue As Variant
    
    ' Set the worksheet (change "Report1" to your actual sheet name if needed)
    Set ws = ThisWorkbook.ActiveSheet ' Or specify: Worksheets("Report1")
    
    ' Reference value from M14
    targetValue = ws.Range("M14").Value
    
    ' Find last row with data in column P (or J)
    lastRow = ws.Cells(ws.Rows.Count, "P").End(xlUp).Row
    
    ' Optional: Unhide all rows before running (remove the comment if desired)
    ' ws.Rows("16:" & lastRow).Hidden = False
    
    ' Loop from row 16 downwards
    For i = 16 To lastRow
        ' Condition: P(i) equals M14 AND J(i) > 0
        If ws.Cells(i, "P").Value = targetValue And ws.Cells(i, "J").Value > 0 Then
            ' If condition met: show the row (unhide)
            ws.Rows(i).Hidden = False
        Else
            ' Otherwise: hide the row
            ws.Rows(i).Hidden = True
        End If
    Next i
End Sub

Re: โค้ด filterRows

Posted: Sun Apr 12, 2026 1:40 pm
by snasui
:D ลองเปลี่ยน

For i = 16 To lastRow เป็น For lastRow = 16 To i Step - 1

เพื่อรันแบบถอยหลังแทนดูก่อนครับ