: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

ต้องการบันทึกข้อมูลให้อยู่ในแถวเดิม

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
chaiwat chinthao
Member
Member
Posts: 3
Joined: Thu Aug 09, 2018 10:22 pm

ต้องการบันทึกข้อมูลให้อยู่ในแถวเดิม

#1

Post by chaiwat chinthao »

หาข้อมูลจากเลขที่ประเมินใน Sheet "db_Incomming Cars All"ด้วยปุ่ม"ค้นหา" เพื่อแก้ไขข้อมูลและบันทึกกลับแถวเดิมใน Sheet "db_Incomming Cars All"ด้วยปุ่ม "อัพเดทข้อมูล"
*ติดปัญหาคือ.อัพเดทข้อมูล.แล้วข้อมูลไปต่อแถวสุดท้ายไม่ทับแถวเดิมครับ
แนบ Code

Code: Select all

Private Sub CommandButton3_Click()
 Dim irow As Long
    Dim ws As Worksheet
    On Error Resume Next
    If Err.Number = 13 Then
MsgBox "ไม่พบข้อมูล"
GoTo iNet
    End If
    If TextBox1.Text = "" Then
MsgBox "กรุณากรอกข้อมูล"
GoTo iNet
 End If
            Set ws = Worksheets("db_Incomming Cars All")
      irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
       If Application.CountIf(ws.Range("b:b"), TextBox1.Value) > 0 Then
        irow = Application.Match(TextBox1.Value, ws.Range("b:b"), 0)
Else
        irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    End If
    
        ws.Cells(irow, 2) = TextBox1.Value
        ws.Cells(irow, 3) = TextBox2.Value
        ws.Cells(irow, 4) = TextBox3.Value
        ws.Cells(irow, 5) = TextBox4.Value
        ws.Cells(irow, 6) = TextBox5.Value
        ws.Cells(irow, 7) = TextBox6.Value
        ws.Cells(irow, 8) = TextBox7.Value
        ws.Cells(irow, 9) = TextBox8.Value
        ws.Cells(irow, 10) = TextBox9.Value
        ws.Cells(irow, 11) = TextBox10.Value
        ws.Cells(irow, 12) = TextBox11.Value
        ws.Cells(irow, 13) = TextBox12.Value
        ws.Cells(irow, 14) = TextBox13.Value
        ws.Cells(irow, 15) = TextBox14.Value
        ws.Cells(irow, 16) = TextBox15.Value
        ws.Cells(irow, 17) = TextBox16.Value
        ws.Cells(irow, 18) = TextBox17.Value
        ws.Cells(irow, 19) = TextBox18.Value
        ws.Cells(irow, 20) = TextBox19.Value
        ws.Cells(irow, 21) = TextBox20.Value
        ws.Cells(irow, 22) = TextBox21.Value
        
        TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
TextBox7.Text = " "
TextBox8.Text = " "
TextBox9.Text = " "
TextBox10.Text = " "
TextBox11.Text = " "
TextBox12.Text = " "
TextBox13.Text = " "
TextBox14.Text = " "
TextBox16.Text = " "
TextBox15.Text = " "
TextBox16.Text = " "
TextBox17.Text = " "
TextBox18.Text = " "
TextBox19.Text = " "
TextBox20.Text = " "
TextBox21.Text = " "
        
        MsgBox "อัพเดทข้อมูลแล้ว"
        ActiveWorkbook.Save
iNet:
End Sub
You do not have the required permissions to view the files attached to this post.
Last edited by snasui on Fri Aug 17, 2018 8:30 pm, edited 1 time in total.
Reason: ปรับให้แสดงเป็น Code
User avatar
snasui
Site Admin
Site Admin
Posts: 31257
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ต้องการบันทึกข้อมูลให้อยู่ในแถวเดิม

#2

Post by snasui »

:D การโพสต์ Code ให้ดูได้ที่กฎการใช้บอร์ดข้อ 5 ด้านบนจะมีตัวอย่าง ตามโพสต์ #1 ผมปรับให้แสดงเป็น Code ให้แล้วครับ

สำหรับการปรับ Code ดูตัวอย่างตามด้านล่างครับ

Code: Select all

'Other code
Set ws = Worksheets("db_Incomming Cars All")
'    irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

If Application.CountIf(ws.Range("b:b"), TextBox1.Value) > 0 Then
    irow = Application.Match(CDbl(TextBox1.Value), ws.Range("b:b"), 0)
Else
    irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
End If
'Other code
chaiwat chinthao
Member
Member
Posts: 3
Joined: Thu Aug 09, 2018 10:22 pm

Re: ต้องการบันทึกข้อมูลให้อยู่ในแถวเดิม

#3

Post by chaiwat chinthao »

สอบถามเพิ่มเติมครับ ผมใช้CommandButton2ในการค้นหาข้อมูลจาก TextBox1 และTextBox4 ติดปัญหาคือผมค้นหาข้อมูลจาก TextBox4 ได้แค่ครั้งเดียว ถ้าผมจะค้นหาข้อมูลจาก TextBox4 อีก ต้องปิด Usre Form แล้วเปิดใหม่ ผมต้องการค้นหาได้ต่อเนื่องโดยไม่ต้องปิด UserForm ครับ

Code: Select all

Private Sub CommandButton2_Click()
Dim nRow As String
On Error Resume Next
'Err.Clear
If TextBox1.Text <> "" Then
eRow = Worksheets("db_Incomming Cars All").Columns(2).Find(TextBox1.Text).Row
nRow = eRow
 ElseIf TextBox4.Text <> "" Then
 fRow = Worksheets("db_Incomming Cars All").Columns(5).Find(TextBox4.Text).Row
nRow = fRow
Else
MsgBox "ใส่ข้อมูลช่องเลขที่ประเมินหรือเลขทะเบียน"
GoTo nNext
  End If

If Err.Number = 91 Then
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
TextBox7.Text = " "
TextBox8.Text = " "
TextBox9.Text = " "
TextBox10.Text = " "
TextBox11.Text = " "
TextBox12.Text = " "
TextBox13.Text = " "
TextBox14.Text = " "
TextBox16.Text = " "
TextBox15.Text = " "
TextBox16.Text = " "
TextBox17.Text = " "
TextBox18.Text = " "
TextBox19.Text = " "
TextBox20.Text = " "
TextBox21.Text = " "
MsgBox "ไม่พบข้อมูล"
GoTo nNext
End If
 
TextBox1.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 2)
TextBox2.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 3)
TextBox3.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 4)
TextBox4.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 5)
TextBox5.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 6)
TextBox6.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 7)
TextBox7.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 8)
TextBox8.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 9)
TextBox9.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 10)
TextBox10.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 11)
TextBox11.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 12)
TextBox12.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 13)
TextBox13.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 14)
TextBox14.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 15)
TextBox15.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 16)
TextBox16.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 17)
TextBox17.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 18)
TextBox18.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 19)
TextBox19.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 20)
TextBox20.Value = Worksheets("db_Incomming Cars All").Cells(nRow, 21)
Sheets("db_Incomming Cars All").Cells(nRow, 1).Activate
nNext:
End Sub
แนบไฟล์ครับ ขอบคุณครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31257
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ต้องการบันทึกข้อมูลให้อยู่ในแถวเดิม

#4

Post by snasui »

:D เพิ่มการประกาศตัวแปร dflt ไว้บนสุดของ Module

Code: Select all

Dim dflt As String
เพิ่ม Procedure 2 ชุดตามด้านล่าง

Code: Select all

Private Sub TextBox4_AfterUpdate()
    dflt = "Tb4"
End Sub

Private Sub TextBox1_AfterUpdate()
    dflt = "Tb1"
End Sub
แก้ไข Code สำหรับการค้นหาเป็นดานล่าง

Code: Select all

'Other code
If dflt = "Tb1" Then
    eRow = Worksheets("db_Incomming Cars All").Columns(2).Find(TextBox1.Text).Row
    nRow = eRow
ElseIf dflt = "Tb4" Then
    fRow = Worksheets("db_Incomming Cars All").Columns(5).Find(TextBox4.Text).Row
    nRow = fRow
Else
'Other code
Post Reply