Page 1 of 1

VBA เพื่อบันทึกข้อมูล

Posted: Fri Aug 26, 2016 4:59 pm
by nopparuj
เรียน อาจารย์ และผู้รู้

ผมสร้าง user form และเขียนโค้ด เพื่อให้บันทึกข้อมูลต่อไปเรื่อยๆโดยไม่ซ้ำบรรทัดกัน แต่ไม่สามารถทำได้ตามความประสงค์
เมื่อบันทึกข้อมูลแล้ว และทำการคีย์เพิ่มเพื่อบันทึกข้อมูลอื่นต่อ แต่จะถูกบันทึกซ้ำในบรรทัดเดียวกัน
รบกวนท่านผู้รู้ช่วยแนะนำและแก้ไขให้ด้วยครับ
ขอแสดงความนับถือ

Code: Select all

Private Sub CommandButton1_Click()
Do
r = r + 1
Loop Until Cells(r, 26) = ""
Cells(r, 2) = TextBox1.Text
Cells(r, 3) = TextBox2.Text
Cells(r, 4) = TextBox3.Text
Cells(r, 5) = TextBox4.Text
Cells(r, 6) = TextBox5.Text
Cells(r, 7) = TextBox6.Text
Cells(r, 8) = TextBox7.Text
Cells(r, 9) = TextBox8.Text
Cells(r, 12) = TextBox9.Text
Cells(r, 13) = TextBox10.Text
Cells(r, 14) = TextBox11.Text
MsgBox "บันทึกข้อมูลเรียบร้อย"

          response = MsgBox("ต้องการป้อนข้อมูลต่อหรือไม่?", _
              vbYesNo)


          If response = vbYes Then
              TextBox1.Text = ""
              TextBox2.Text = ""
              TextBox3.Text = ""
              TextBox4.Text = ""
              TextBox5.Text = ""
              TextBox6.Text = ""
              TextBox7.Text = ""
              TextBox8.Text = ""
              TextBox9.Text = ""
              TextBox10.Text = ""
              TextBox11.Text = ""

              TextBox1.SetFocus

          Else
              Unload Me
          End If

      End Sub

Re: VBA เพื่อบันทึกข้อมูล

Posted: Fri Aug 26, 2016 7:25 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Private Sub CommandButton1_Click()
'    Do
'        r = r + 1
'    Loop Until Cells(r, 26) = ""
    Dim r As Long
    
    r = Cells(Rows.Count, 2).End(xlUp).Row + 1
    'Other Code
End Sub

Re: VBA เพื่อบันทึกข้อมูล

Posted: Sun Aug 28, 2016 1:05 pm
by nopparuj
เรียน ท่านอาจารย์
ผมลองปรับโค้ดแล้ว ใช้ได้ตรงตามจุดประสงค์ครับ ขอบคุณครับ
ขอแสดงความนับถือ