userform show edit and delete
Posted: Tue Jun 02, 2020 11:13 pm

ติดปัญหาการสร้าง userform ที่ แสดงรายการข้อมูล การแก้ไขและลบรายการ การบันทึกรายการ ใน userform1 และ userform2 จึ่งขออธิบายดังนี้
1.ใน Useform1
1.1 ต้องการกำหนดให้(ที่ระบายสีไว้)
การทำงานคือแสดงรายการ column G , H , I ได้แล้ว แต่ต้องการกำหนดช่วงข้อมูล
ในลักษณะ
Code: Select all
LastRow = Sheets("edit and delete").Range("G" & Rows.Count).End(xlUp).Row
Code: Select all
ListBox1.RowSource = "G2:I2000"
Code: Select all
Private Sub CommandButton1_Click() 'Show Data
ListBox1.ColumnCount = 3
ListBox1.RowSource = "G2:I2000"
End Sub
จะปรับ ListBox1.RowSource = "" อย่างไรครับ
1.2 ComboBox1.List "ไม่มี"
Code: Select all
Private Sub ComboBox1_Change()
For i = 2 To Application.WorksheetFunction.CountA(Sheets("edit and delete").Range("G:G"))
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
Me.ListBox1.AddItem Cells(i, 1).Value
Me.ListBox1.AddItem Cells(i, 2).Value
Me.ListBox1.AddItem Cells(i, 3).Value
End If
Next i
End Sub
เป็น ComboBox1.List เลือกข้อความว่า "ไม่มี"ได้แล้ว แสดงรายการใน Listbox
แต่พอ Loop แล้ว กลับไม่แสดงรายการที่เลือกข้อความว่า "ไม่มี"
เช่น
ชื่อ แผนก สถานะ
BBB2 bbb2 ไม่มี
หาไม่เจอสถานะว่า ไม่มี ครับ
Code: Select all
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
ต้องการให้แสดงรายการ ชื่อ แผนก สถานะ ที่มีสถานะว่า "ไม่มี" ครับ
2.ใน Useform2
เมื่อทำการกดปุ่ม edit selected แล้ว จะไปเปิด Useform2 เพื่อทำการ ดังนี้
2.1 save ต้องการให้ทุกครั้งที่ save ให้ข้อมูลต่อลงมาที่บรรทัดสุดท้าย
แต่ code กลับ ไป edit selected ที่ row ที่ 3 จาก column G , H ไป column B , C
Code: Select all
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
Code: Select all
Private Sub CommandButton1_Click() 'save
Dim i As Integer
For i = 2 To Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 7) = Label4.Caption Then
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
End If
Next i
MsgBox "Saved!", vbInformation
'Unload Me
End Sub
ลบได้ตามต้องการแต่กลับไปกระทบกับ Cell อื่น
ไม่ไปกระทบกับช่อง Cell อื่น
เนื่องจากใน cloumn B และ C มีการอ้างอิงจาก cell อื่น (ข้อมูลที่ใช้จริงมีสูตรอยู่) เมื่อลบไปแล้วทำให้
สูตรที่อ้างอิงไว้ขึ้น #Ref
ที่ต้องการคือ ลบรายการนั้นแล้วไม่ทำให้สูตรอ้างอิงหายไปครับ ช่วยปรับเป็นตัวอย่างให้ด้วยครับ
Code: Select all
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
Code: Select all
Private Sub DeleteSelected_Click()
Dim i As Integer
If MsgBox("Are you sure you want to delete the row?", vbYesNo + vbQuestion, "Delete row") = vbYes Then
For i = 1 To Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Cells(i, 2).Select
Cells(i, 2).Value = ""
Cells(i, 2).Select
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
End If
Next i
End If
End Sub
ขอบคุณครับ