EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)snasui wrote: Sat Jun 06, 2020 8:57 amเปลี่ยนจาก
Selection.delete
เป็นSelection.clearcontents
จะได้ไม่ทำให้เซลล์ที่เกี่ยวข้องติด #REF ครับ
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
Code: Select all
Range("B" & i + 1, "C" & lastrow).Copy Destination: Sheets("edit and delete").Range("B" & i, "C" & i).Value
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
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
Code: Select all
Cells(i, 2).Resize(1, 2).delete shift:=xlUp
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
snasui wrote: Sat Jun 06, 2020 5:30 pmในโอกาสหน้ากรุณายกตัวอย่างให้เหมือนกับสิ่งที่เป็นปัญหาครับ
ตัวอย่าง Code ตามด้านล่างครับ
ควรหลีกเลี่ยงการใช้สูตรอ้างอิงตำแหน่งตรง ๆ กับฐานข้อมูลที่มีการ Delete, Insert หรือหากจำเป็นที่จะทำเช่นนั้นให้เลือกใช้ฟังก์ชั่น Indirect เข้ามาช่วยได้ ยกตัวอย่างเช่น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
เซลล์ E9 คีย์เป็น =Indirect("B9") เช่นนี้เป็นต้น