
เรียนอาจารย์ ผมปรับมาแล้วครับ อธิบายดังนี้
Code: Select all
Private Sub DeleteSelected_Click()
Dim i, lastrow As Long
lastrow = Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If MsgBox("Are you sure you want to delete the row?", vbYesNo + vbQuestion, "Delete row") = vbYes Then
For i = 1 To lastrow
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Range("B" & i, "C" & i).Select
Range("B" & i, "C" & i).ClearContents
Range("B" & i + 1, "C" & lastrow).Select
Range("B" & i + 1, "C" & lastrow).Copy Destination: Sheets("edit and delete").Range("B" & i, "C" & i).Value
Range("B" & i + lastrow, "C" & lastrow).ClearContents
End If
Next i
End If
End Sub
เมื่อ Range ข้อมูลที่ต้องการลบออกได้แล้ว
จึงทำการ copy ข้อมูลที่อยู่แถวใดๆใน column B : C ไปจนถึงบรรทัดสุดท้ายที่มีทั้งหมด ขยับขึ้นมาวางไว้ในข้อมูลที่ได้ลบไปแล้ว หลังจากนั้น
จึงลบบรรทัดสุดท้ายที่ซ้ำออกไปครับ
เช่น เมื่อลบข้อมูล ชื่อ GGG แผนก ggg1 แล้ว ทำการ copy ข้อมูลจาก HHH hhh1 ไปถึงข้อมูลสุดท้าย คือ III iii1 ขยับขึ้นมาวางต่อกันที่
ช่อง B8:C8 แล้วจึงลบ ข้อมูลสุดท้าย คือ III iii1 ซ้ำออกครับ แต่ติดปัญหาที่
Code: Select all
Range("B" & i + 1, "C" & lastrow).Copy Destination: Sheets("edit and delete").Range("B" & i, "C" & i).Value
ตรงนี้ครับ ขอบคุณครับ
edit and delete.xlsm
You do not have the required permissions to view the files attached to this post.