Page 2 of 2
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 10:14 am
by Totem
snasui wrote: Sat Jun 06, 2020 8:57 am

เปลี่ยนจาก
Selection.delete
เป็น
Selection.clearcontents
จะได้ไม่ทำให้เซลล์ที่เกี่ยวข้องติด #REF ครับ

ลบได้ตามต้องการ แต่อยากให้ทุกครั้งที่มีการลบ ข้อมูลทางอยู่ด้านล่างเลื่อนขึ้นมาแท้ที่จนครบถึงบรรทัดสุดท้ายครับ
เมื่อจะเพิ่มข้อมูลจะได้ไปต่อที่บรรทัดสุดท้ายครับ
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 10:32 am
by snasui

คำถามคือต้องการลบทั้งบรรทัดของรายการนั้น ๆ ใช่หรือไม่ครับ
ถ้าใช่ตอน Delete ให้เลือก Range ของทั้งรายการ ไม่ใช่เซลล์เดียว ลองปรับมาเองก่อน ติดแล้วค่อยถามกันครับ
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 12:44 pm
by Totem

เรียนอาจารย์ ผมปรับมาแล้วครับ อธิบายดังนี้
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
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 1:05 pm
by snasui

ตัวอย่างการปรับ Code ครับ
Code: Select all
'Other code
For i = 1 To lastrow
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Cells(i, 2).Resize(1, 2).delete shift:=xlUp
Exit For
End If
Next i
'Other code
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 4:50 pm
by Totem
snasui wrote: Sat Jun 06, 2020 1:05 pm

ตัวอย่างการปรับ Code ครับ
Code: Select all
'Other code
For i = 1 To lastrow
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Cells(i, 2).Resize(1, 2).delete shift:=xlUp
Exit For
End If
Next i
'Other code

เรียนอาจารย์ ปรับแล้วเมื่อ Resize แล้ว delete ทำให้เกิด #Ref ครับ
เป็นเพราะใน column B , C ถูกใช้อ้างอิงกับสูตรครับจึงให้ ช่องที่ใช้สูตร เกิด #Ref สูตรหายครับ
ตัวอย่างเช่น จะลบ ชื่อ HHH แผนก hhh1 และมีสูตรอยู่ ช่อง E , F เป็นตัน (ข้อมูลจริงอ้างอิงทั้ง column B , C ทั้ง column) ลบแล้ว จึงทำให้ ช่อง E , F เป็นค่า #Ref ครับ
การ ClearContents แล้วเลื่อนขึ้น ทำให้สูตรที่ผมสร้างขึ้นไม่หายครับ
ช่วยปรับตรงนี้ครับ
Code: Select all
Cells(i, 2).Resize(1, 2).delete shift:=xlUp
edit and delete.xlsm
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 5:30 pm
by snasui

ในโอกาสหน้ากรุณายกตัวอย่างให้เหมือนกับสิ่งที่เป็นปัญหาครับ
ตัวอย่าง Code ตามด้านล่างครับ
Code: Select all
'Other code
Dim temp as Variant
For i = 1 To lastrow
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
If Cells(i + 1, 2).Value <> "" Then
temp = Range(Cells(i + 1, 2), Cells(Rows.Count, 2).End(xlUp)).Resize(, 2)
Range(Cells(i, 2), Cells(Rows.Count, 3).End(xlUp)).ClearContents
Cells(i, 2).Resize(UBound(temp, 1), 2).Value = temp
Else
Range(Cells(i, 2), Cells(Rows.Count, 3).End(xlUp)).ClearContents
End If
Exit For
End If
Next i
'Other code
ควรหลีกเลี่ยงการใช้สูตรอ้างอิงตำแหน่งตรง ๆ กับฐานข้อมูลที่มีการ Delete, Insert หรือหากจำเป็นที่จะทำเช่นนั้นให้เลือกใช้ฟังก์ชั่น Indirect เข้ามาช่วยได้ ยกตัวอย่างเช่น
เซลล์ E9 คีย์เป็น
=Indirect("B9") เช่นนี้เป็นต้น
Re: userform show edit and delete
Posted: Sat Jun 06, 2020 6:00 pm
by Totem
snasui wrote: Sat Jun 06, 2020 5:30 pm

ในโอกาสหน้ากรุณายกตัวอย่างให้เหมือนกับสิ่งที่เป็นปัญหาครับ
ตัวอย่าง Code ตามด้านล่างครับ
Code: Select all
'Other code
Dim temp as Variant
For i = 1 To lastrow
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
If Cells(i + 1, 2).Value <> "" Then
temp = Range(Cells(i + 1, 2), Cells(Rows.Count, 2).End(xlUp)).Resize(, 2)
Range(Cells(i, 2), Cells(Rows.Count, 3).End(xlUp)).ClearContents
Cells(i, 2).Resize(UBound(temp, 1), 2).Value = temp
Else
Range(Cells(i, 2), Cells(Rows.Count, 3).End(xlUp)).ClearContents
End If
Exit For
End If
Next i
'Other code
ควรหลีกเลี่ยงการใช้สูตรอ้างอิงตำแหน่งตรง ๆ กับฐานข้อมูลที่มีการ Delete, Insert หรือหากจำเป็นที่จะทำเช่นนั้นให้เลือกใช้ฟังก์ชั่น Indirect เข้ามาช่วยได้ ยกตัวอย่างเช่น
เซลล์ E9 คีย์เป็น
=Indirect("B9") เช่นนี้เป็นต้น

ครับ

ขอบคุณครับ ได้ตามต้องการ ครับ