Page 1 of 1

การลบแถวที่มีข้อมูลตามคำที่ค้นหา

Posted: Fri Nov 04, 2016 4:03 pm
by Kanok
ต้องการลบทุกแถวที่มีข้อมูลที่ขี้นต้นด้วย PS ค่ะ

Code: Select all

Sub deletePS()

lastrow = Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row

For i = 2 To lastrow

If Rows(i).Find("PS") Then
Rows(i).Delete

End If

Next i

End Sub

Delete PS.xlsm

ติดปัญหาไม่สาารถรันได้ค่ะ
รบกวนขอคำชี้แนะด้วยค่ะ

Re: การลบแถวที่มีข้อมูลตามคำที่ค้นหา

Posted: Fri Nov 04, 2016 4:24 pm
by niwat2811
ลองปรับ Code เป็นแบบนี้ดูครับ

Code: Select all

Sub deletePS()

lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = lastrow To 2 Step -1
    If Left(Range("A" & i).Value, 2) = "PS" Then
        Range("A" & i).EntireRow.Delete
    End If
Next i

End Sub

Re: การลบแถวที่มีข้อมูลตามคำที่ค้นหา

Posted: Fri Nov 04, 2016 4:37 pm
by Kanok
niwat2811 wrote:ลองปรับ Code เป็นแบบนี้ดูครับ

Code: Select all

Sub deletePS()

lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = lastrow To 2 Step -1
    If Left(Range("A" & i).Value, 2) = "PS" Then
        Range("A" & i).EntireRow.Delete
    End If
Next i

End Sub


รันได้แล้วค่ะ

ขอบคุณมากค่ะ :thup: