Page 1 of 1

สอบถาม Code vba บันทึกข้อมูลจากฟอร์ม ลง Excel โดยให้ข้อมูลลง Cell ที่เรากำหนดและต่อกันลงมา

Posted: Tue Apr 10, 2018 9:05 am
by jullathep

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Data")
emptyrow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(emptyrow, 1).Value = UserForm1.TextBox1.Text
ws.Cells(emptyrow, 2).Value = UserForm1.TextBox2.Text
ws.Cells(emptyrow, 3).Value = UserForm1.TextBox3.Text
ws.Cells(emptyrow, 4).Value = UserForm1.TextBox4.Text
ws.Cells(emptyrow, 5).Value = UserForm1.ComboBox1.Text
ws.Cells(emptyrow, 6).Value = UserForm1.TextBox6.Text

Sheet1.Range("B10") = UserForm1.TextBox2.Text
Sheet1.Range("F10") = UserForm1.TextBox4.Text
Sheet1.Range("G10") = UserForm1.ComboBox1.Text
Sheet1.Range("H10") = UserForm1.TextBox6.Text
คือผมอยากให้ ตั้งแต่ B10-H10 ข้อมูลที่ถูกส่งมาจากฟอร์ม บันทึกต่อกันลง มา ผมจะต้องเขียน Code ต่อว่าอย่างไรครับ ส่วนด้านบน ผมบันทึกลง Sheet Data หากอาจารย์ท่านใดเห็นว่ายังไม่เหมาะเท่าที่ควร ก็ ขอคำชี้แนะด้วยครับขอบคุณครับ

Re: สอบถาม Code vba บันทึกข้อมูลจากฟอร์ม ลง Excel โดยให้ข้อมูลลง Cell ที่เรากำหนดและต่อกันลงมา

Posted: Tue Apr 10, 2018 3:16 pm
by puriwutpokin
jullathep wrote: Tue Apr 10, 2018 9:05 am

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Data")
emptyrow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(emptyrow, 1).Value = UserForm1.TextBox1.Text
ws.Cells(emptyrow, 2).Value = UserForm1.TextBox2.Text
ws.Cells(emptyrow, 3).Value = UserForm1.TextBox3.Text
ws.Cells(emptyrow, 4).Value = UserForm1.TextBox4.Text
ws.Cells(emptyrow, 5).Value = UserForm1.ComboBox1.Text
ws.Cells(emptyrow, 6).Value = UserForm1.TextBox6.Text

Sheet1.Range("B10") = UserForm1.TextBox2.Text
Sheet1.Range("F10") = UserForm1.TextBox4.Text
Sheet1.Range("G10") = UserForm1.ComboBox1.Text
Sheet1.Range("H10") = UserForm1.TextBox6.Text
คือผมอยากให้ ตั้งแต่ B10-H10 ข้อมูลที่ถูกส่งมาจากฟอร์ม บันทึกต่อกันลง มา ผมจะต้องเขียน Code ต่อว่าอย่างไรครับ ส่วนด้านบน ผมบันทึกลง Sheet Data หากอาจารย์ท่านใดเห็นว่ายังไม่เหมาะเท่าที่ควร ก็ ขอคำชี้แนะด้วยครับขอบคุณครับ
ลองปรับเป็น

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet, wss As Worksheet
Set ws = Worksheets("Data")
emptyrow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(emptyrow, 1).Value = UserForm1.TextBox1.Text
ws.Cells(emptyrow, 2).Value = UserForm1.TextBox2.Text
ws.Cells(emptyrow, 3).Value = UserForm1.TextBox3.Text
ws.Cells(emptyrow, 4).Value = UserForm1.TextBox4.Text
ws.Cells(emptyrow, 5).Value = UserForm1.ComboBox1.Text
ws.Cells(emptyrow, 6).Value = UserForm1.TextBox6.Text
Set wss = Worksheets("Report")
emptyrows = WorksheetFunction.CountA(wss.Range("B:B")) + 9
wss.Cells(emptyrows, 2).Value = UserForm1.TextBox2.Text
wss.Cells(emptyrows, 6).Value = UserForm1.TextBox4.Text
wss.Cells(emptyrows, 7).Value = UserForm1.ComboBox1.Text
wss.Cells(emptyrows, 8).Value = UserForm1.TextBox6.Text
End Sub

Re: สอบถาม Code vba บันทึกข้อมูลจากฟอร์ม ลง Excel โดยให้ข้อมูลลง Cell ที่เรากำหนดและต่อกันลงมา

Posted: Tue Apr 10, 2018 3:19 pm
by jullathep
puriwutpokin wrote: Tue Apr 10, 2018 3:16 pm
jullathep wrote: Tue Apr 10, 2018 9:05 am

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Data")
emptyrow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(emptyrow, 1).Value = UserForm1.TextBox1.Text
ws.Cells(emptyrow, 2).Value = UserForm1.TextBox2.Text
ws.Cells(emptyrow, 3).Value = UserForm1.TextBox3.Text
ws.Cells(emptyrow, 4).Value = UserForm1.TextBox4.Text
ws.Cells(emptyrow, 5).Value = UserForm1.ComboBox1.Text
ws.Cells(emptyrow, 6).Value = UserForm1.TextBox6.Text

Sheet1.Range("B10") = UserForm1.TextBox2.Text
Sheet1.Range("F10") = UserForm1.TextBox4.Text
Sheet1.Range("G10") = UserForm1.ComboBox1.Text
Sheet1.Range("H10") = UserForm1.TextBox6.Text
คือผมอยากให้ ตั้งแต่ B10-H10 ข้อมูลที่ถูกส่งมาจากฟอร์ม บันทึกต่อกันลง มา ผมจะต้องเขียน Code ต่อว่าอย่างไรครับ ส่วนด้านบน ผมบันทึกลง Sheet Data หากอาจารย์ท่านใดเห็นว่ายังไม่เหมาะเท่าที่ควร ก็ ขอคำชี้แนะด้วยครับขอบคุณครับ
ลองปรับเป็น

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet, wss As Worksheet
Set ws = Worksheets("Data")
emptyrow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(emptyrow, 1).Value = UserForm1.TextBox1.Text
ws.Cells(emptyrow, 2).Value = UserForm1.TextBox2.Text
ws.Cells(emptyrow, 3).Value = UserForm1.TextBox3.Text
ws.Cells(emptyrow, 4).Value = UserForm1.TextBox4.Text
ws.Cells(emptyrow, 5).Value = UserForm1.ComboBox1.Text
ws.Cells(emptyrow, 6).Value = UserForm1.TextBox6.Text
Set wss = Worksheets("Report")
emptyrows = WorksheetFunction.CountA(wss.Range("B:B")) + 9
wss.Cells(emptyrows, 2).Value = UserForm1.TextBox2.Text
wss.Cells(emptyrows, 6).Value = UserForm1.TextBox4.Text
wss.Cells(emptyrows, 7).Value = UserForm1.ComboBox1.Text
wss.Cells(emptyrows, 8).Value = UserForm1.TextBox6.Text
End Sub
ชอบคุณมากๆครับ