:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

userform show edit and delete

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
Totem
Silver
Silver
Posts: 650
Joined: Fri Oct 11, 2013 7:52 pm
Excel Ver: 365 , 2007

Re: userform show edit and delete

#21

Post by Totem »

snasui wrote: Sat Jun 06, 2020 8:57 am :D เปลี่ยนจาก Selection.delete เป็น Selection.clearcontents จะได้ไม่ทำให้เซลล์ที่เกี่ยวข้องติด #REF ครับ
:D ลบได้ตามต้องการ แต่อยากให้ทุกครั้งที่มีการลบ ข้อมูลทางอยู่ด้านล่างเลื่อนขึ้นมาแท้ที่จนครบถึงบรรทัดสุดท้ายครับ
เมื่อจะเพิ่มข้อมูลจะได้ไปต่อที่บรรทัดสุดท้ายครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31032
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: userform show edit and delete

#22

Post by snasui »

:lol: คำถามคือต้องการลบทั้งบรรทัดของรายการนั้น ๆ ใช่หรือไม่ครับ

ถ้าใช่ตอน Delete ให้เลือก Range ของทั้งรายการ ไม่ใช่เซลล์เดียว ลองปรับมาเองก่อน ติดแล้วค่อยถามกันครับ
Totem
Silver
Silver
Posts: 650
Joined: Fri Oct 11, 2013 7:52 pm
Excel Ver: 365 , 2007

Re: userform show edit and delete

#23

Post by Totem »

:D เรียนอาจารย์ ผมปรับมาแล้วครับ อธิบายดังนี้

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.
User avatar
snasui
Site Admin
Site Admin
Posts: 31032
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: userform show edit and delete

#24

Post by snasui »

:D ตัวอย่างการปรับ 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
Totem
Silver
Silver
Posts: 650
Joined: Fri Oct 11, 2013 7:52 pm
Excel Ver: 365 , 2007

Re: userform show edit and delete

#25

Post by Totem »

snasui wrote: Sat Jun 06, 2020 1:05 pm :D ตัวอย่างการปรับ 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
:D เรียนอาจารย์ ปรับแล้วเมื่อ 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
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31032
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: userform show edit and delete

#26

Post by snasui »

:lol: ในโอกาสหน้ากรุณายกตัวอย่างให้เหมือนกับสิ่งที่เป็นปัญหาครับ

ตัวอย่าง 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") เช่นนี้เป็นต้น
Totem
Silver
Silver
Posts: 650
Joined: Fri Oct 11, 2013 7:52 pm
Excel Ver: 365 , 2007

Re: userform show edit and delete

#27

Post by Totem »

snasui wrote: Sat Jun 06, 2020 5:30 pm :lol: ในโอกาสหน้ากรุณายกตัวอย่างให้เหมือนกับสิ่งที่เป็นปัญหาครับ

ตัวอย่าง 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") เช่นนี้เป็นต้น
:D :) ครับ
:D ขอบคุณครับ ได้ตามต้องการ ครับ
Post Reply