รบกวนตรวจสอบ Code ใน Userform 1 คำสั่ง:เมื่อค้นหาข้อมูลจาก sheet DataInput จากนั้นทำการแก้ไขข้อมูลและบันทึก โดยกดปุ่ม"บันทึกข้อมูลการสั่งซื้อ" ข้อมูลจะบันทึกในบรรทัดถัดไป ไม่แก้ไขข้อมูลในแถวที่เลือกค่ะ ซึ่งต้องการให้ข้อมูลบันทึกลงแถวเดิมค่ะ และ ใน UserFom 1 เมื่อคลิกปุ่มถัดไปและก่อนหน้าก็ ข้อมูลที่แสดงไม่ตรงกับแถวที่อยู่ถัดไปและก่อนหน้าคะ
ตัวอย่างตามไฟล์แนบค่ะ
Code: Select all
Private Sub CommandButton2_Click()
Dim irow As Long
Dim ws As Worksheet
Set ws = Worksheets("DataInput")
'find first empty row in database
irow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
'Check for Id
If (Me.Textpo.Text) = "" Then
Me.Textpo.SetFocus
Exit Sub
End If
If Application.CountIf(Range("c:c"), Textpo.Text) > 0 Then
irow = Application.Match((Textpo.Text), Range("c:c"), 0)
msgRepns = MsgBox("เลขที่ PO นี้มีอยู่แล้วถ้าต้องการแก้ไขข้อมูลคลิก Yes หากต้องการยกเลิกให้คลิก NO", vbYesNo)
Else
irow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
End If
If msgRepns = vbNo Then
Exit Sub
End If
'copy the data to the database
ws.Cells(irow, 3).Value = Me.Textpo.Value
ws.Cells(irow, 4).Value = Me.Textpr.Value
ws.Cells(irow, 5).Value = Format(Me.Textdate.Value, "dd-mmm-yyyy")
ws.Cells(irow, 6).Value = Me.ComboBox1.Value
ws.Cells(irow, 7).Value = Me.TextSup.Value
ws.Cells(irow, 8).Value = Me.TextPrice.Value
ws.Cells(irow, 9).Value = Me.Textqty.Value
ws.Cells(irow, 10).Value = Me.Textqtyrec.Value
ws.Cells(irow, 11).Value = Format(Me.TextDue.Value, "dd-mmm-yyyy")
ws.Cells(irow, 12).Value = Format(Me.Textdaterec.Value, "dd-mmm-yyyy")
Unload Me
'Clear the data
Me.Textpo.Value = ""
Me.Textpr.Value = ""
Me.Textdate.Value = ""
Me.ComboBox1.Value = ""
Me.TextSup.Value = ""
Me.TextPrice.Value = ""
Me.Textqty.Value = ""
Me.Textqtyrec.Value = ""
Me.TextDue.Value = ""
Me.Textdaterec.Value = ""
MsgBox "บันทึกข้อมูลสำเร็จ"
End Sub
ขอบคุณมากค่ะ
พยายามศึกษาจากกระทู้เดิมแล้วและปรับแก้ตามแล้ว แต่ยังไม่ได้ค่ะ