Page 1 of 1

บันทึกข้อมูลลง sheet

Posted: Wed Mar 06, 2013 1:32 pm
by torie04
รบกวนผู้รู้ VBA ช่วยผมหน่อยครับ ติดปัญหาตรงที่ค้นหาข้อมูลจาก Userform เพื่อนำข้อมูลมาแก้ไข แล้วจะบันทึกทับในตำแหน่งเซลเดิม ไม่ทราบว่าต้องเขียน Code VBA ครับ.

Re: บันทึกข้อมูลลง sheet

Posted: Wed Mar 06, 2013 2:25 pm
by bank9597
:D ไม่ถนััด VBA ครับ เดี๋ยวรอท่านอื่นมาช่วยดูให้อีกทีครับ

Re: บันทึกข้อมูลลง sheet

Posted: Wed Mar 06, 2013 4:41 pm
by tupthai
ใช้codeเดียวกับการค้นหาข้อมูล ซึ่งทำได้อยู่แล้ว ก็คือเมื่อตรวจสอบเจอ ก็เอาข้อมูลแถวนั้นทีละคอลัมน์ ไปใส่ใน Form
การแก้ไขจะกลับกัน เมื่อตรวจสอบเจอเอาข้อมูลใน Formมาใส่ใน cellแทน
ค้นหาข้อมูล ของคุณtorie04 ใช้codeนี้อยู่

Code: Select all

With Worksheets(db_sheet)
    .Cells(blank_row, id_col).Value = form1.id_txtbox.Value
    .Cells(blank_row, name_col).Value = form1.name_txtbox.Value
    .Cells(blank_row, surname_col).Value = form1.surname_txtbox.Value
    .Cells(blank_row, sex_col).Value = form1.sex_cbbox.Value
    .Cells(blank_row, section_col).Value = form1.Section_txtbox.Value
    .Cells(blank_row, Dept_col).Value = form1.Dept_cbbox.Value
    
End With
พอจะแก้ไขข้อมูลใช้codeนี้แทน

Code: Select all

   Worksheets(db_sheet).Cells(data_row, name_col).Value = name_txtbox.Value
   Worksheets(db_sheet).Cells(data_row, surname_col).Value = surname_txtbox.Value
   Worksheets(db_sheet).Cells(data_row, sex_col).Value = sex_cbbox.Value
   Worksheets(db_sheet).Cells(data_row, id_col).Value = id_txtbox.Value
   Worksheets(db_sheet).Cells(data_row, section_col).Value = Section_txtbox.Value
   Worksheets(db_sheet).Cells(data_row, Dept_col).Value = Dept_cbbox.Value
และการใช้ Do Until ถ้าหาไม่เจอจะค้นทั้งคอลัมน์ทำให้ช้า ควรเพิ่ม ให้หยุดเมื่อถึงข้อมูลแถวสุดท้าย

Code: Select all

 lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
 Do Until id_check = id_txtbox.Text Or data_row > lastrow

Re: บันทึกข้อมูลลง sheet

Posted: Thu Mar 07, 2013 9:53 am
by torie04
ขอบคุณท่าน tupthai มากครับ ใช้ได้ตรงกับความต้องการผมเลยครับ.

Re: บันทึกข้อมูลลง sheet

Posted: Fri Mar 08, 2013 11:49 pm
by lotto009
ขอบคุณที่shareความรู้นะครับ

Re: บันทึกข้อมูลลง sheet

Posted: Thu Mar 14, 2013 12:36 am
by tutape
สิ่งที่คุณ tupthai โพสไว้ตรงกับสิ่งที่ผมจะนำไปประยุกต์ใช้ครับ แต่ของผมจะต้องเพิ่มจำนวนข้อมูลในการบันทึก ผมจะต้องไปแก้ไขในส่วนไหนบ้างครับ พอดีว่ามือใหม่ในเรื่อง VBA นิดนึง เลยรบกวนชี้แนะด้วยครับ

Re: บันทึกข้อมูลลง sheet

Posted: Thu Mar 14, 2013 7:48 am
by joo
:D ช่วยแนบไฟล์ ระบุเงื่อนไขและคำตอบที่ต้องการ พร้อมระบุโค๊ดที่ได้ลองทำแล้วที่ติดปัญหามาดูกันหน่อยครับเพื่อเพื่อนๆสมาชิกจะได้ช่วยกันทดสอบได้