Page 2 of 3
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 11:40 am
by Benmore
snasui wrote:
ไฟล์ที่แนบมายังไม่มีการปรับ Code ที่ผมตอบไปด้านบน ปรับมาก่อนแล้วแนบมาใหม่ครับ
Code: Select all
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
If VBA.Left(ct.Name, 4) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 8).Value = ct.Caption
Exit For
End If
Next ct
แก้ไขประมาณนี้ค่ะ
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 2:12 pm
by snasui

แจ้งตัวอย่างข้อมูลทดสอบมาด้วยครับ control ใดกรอกค่าใดบ้างครับ
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 2:28 pm
by Benmore
ภาพ 1
รูป.png
เมื่อเลือกข้อมูลครบแล้วตามภาพ
ภาพที่ 2
342.png
ข้อมูลในวงกลมในภาพที่ 1 ไม่ถูกบันทึกลงในภาพที่สองค่ะ
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 2:32 pm
by snasui

ข้อมูลในวงกลมในภาพยังไม่ถูกเลือก ถ้าจะจับภาพแทนเขียนข้อความ ควรจับให้มีการเลือกข้อมูลทีจำเป็นมาด้วย ไม่เช่นนั้นเขียนแจ้งมาเลยว่า Control ใดเลือกหรือคีย์ใด ข้อมูลพวกนี้เป็นสิ่งจำเป็น ผมจะทดสอบตามนั้นทันทีไม่ต้องใช้เวลานาน
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 2:55 pm
by Benmore
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 5:00 pm
by snasui

เรียงลำดับ Code ไม่ถูกต้องครับ
ตัวอย่างการเรียงลำดับ Code ควรจะเป็นด้านล่าง
Code: Select all
Private Sub btsave_Click()
On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "¡ÃسҡÃÍ¡¢éÍÁÙÅãËé¤Ãº¶éǹ"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
Sheet9.Activate
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
'Other code
สังเกตตัวแปรที่จะกำหนดค่าบรรทัดจะต้องกำหนดเอาไว้ก่อนที่จะนำข้อมูลไปวาง
ควรจะเอา On Error Resume Next ออกเสียก่อนแล้วทำการ Debug จะได้ทราบได้เองเบื้องต้นว่าผิดพลาดหรือไม่อย่างไรครับ
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 6:29 pm
by Benmore
ลองปรับโค้ดตามแล้วไม่เกิดข้อผิดพลาดค่ะแต่ข้อมูลที่เลือกใน option button ไม่ถูกบันทึกลงในชีท 6 ค่ะ
Code: Select all
Private Sub btsave_Click()
'On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "¡ÃسҡÃÍ¡¢éÍÁÙÅãËé¤Ãº¶éǹ"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
Sheet9.Activate
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
If VBA.Left(ct.Name, 4) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 8).Value = ct.Caption
Exit For
End If
Next ct
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
'emptyRow = WorksheetFunction.Count("A3:A10000") + 1
If emptyRow = 0 Then
emptyRow = 2
Else
emptyRow = emptyRow + 2
Sheet9.Activate
strTb1 = Split(TextBox1.Text, vbCrLf)
strTb3 = TextBox3.Text & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = Split(strTb3, vbCrLf)
Cells(emptyRow, 1).Value = VBA.Mid(strTb1(0), InStr(strTb1(0), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 2).Value = VBA.Mid(strTb1(1), InStr(strTb1(1), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 3).Value = VBA.Mid(strTb1(2), InStr(strTb1(2), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 4).Value = VBA.Mid(strTb1(3), InStr(strTb1(3), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 6).Value = strTb3(0) & "," & strTb3(1) & "," & strTb3(2) & vbCrLf & strTb3(3) & "," & strTb3(4) & "," & strTb3(5) 'TextBox3.Value
'Cells(emptyRow, 7).Value = OptionButton
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "ºÑ¹·Ö¡¢éÍÁÙÅàÃÕºÃéÍÂáÅéÇ"
Unload Me
UserForm1.Show
End If
'Sheet1.Activate
End Sub
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 7:33 pm
by snasui

Code ตามโพสต์ #27 เกี่ยวกับ Sheet9 ไม่เกี่ยวกับ Sheet6 ลองทบทวนดูอีกรอบครับ
Re: บันทึกข้อมูล
Posted: Thu Nov 16, 2017 11:32 pm
by Benmore
ชีท9 ค่ะ
มันก็ไม่แสดงค่ะ
Re: บันทึกข้อมูล
Posted: Fri Nov 17, 2017 6:45 am
by snasui

ไฟล์ที่แนบมาล่าสุดยังไม่ปรับ Code เพื่อให้นำค่าใน Option Button ไปวาง ดูรูปประกอบครับ
Re: บันทึกข้อมูล
Posted: Fri Nov 17, 2017 8:47 am
by Benmore
ไฟล์แนบที่แก้ไขโค้ดแล้วรันไม่ได้ตามที่ต้องการค่ะ
Code: Select all
'Save
Private Sub btsave_Click()
On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "¡ÃسҡÃÍ¡¢éÍÁÙÅãËé¤Ãº¶éǹ"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
If VBA.Left(ct.Name, 4) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 8).Value = ct.Caption
Exit For
End If
Next ct
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
'emptyRow = WorksheetFunction.Count("A3:A10000") + 1
If emptyRow = 0 Then
emptyRow = 2
Else
emptyRow = emptyRow + 2
Sheet9.Activate
strTb1 = Split(TextBox1.Text, vbCrLf)
strTb3 = TextBox3.Text & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = Split(strTb3, vbCrLf)
Cells(emptyRow, 1).Value = VBA.Mid(strTb1(0), InStr(strTb1(0), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 2).Value = VBA.Mid(strTb1(1), InStr(strTb1(1), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 3).Value = VBA.Mid(strTb1(2), InStr(strTb1(2), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 4).Value = VBA.Mid(strTb1(3), InStr(strTb1(3), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 6).Value = strTb3(0) & "," & strTb3(1) & "," & strTb3(2) & vbCrLf & strTb3(3) & "," & strTb3(4) & "," & strTb3(5) 'TextBox3.Value
'Cells(emptyRow, 7).Value = OptionButton
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "ºÑ¹·Ö¡¢éÍÁÙÅàÃÕºÃéÍÂáÅéÇ"
Unload Me
UserForm1.Show
End If
Sheet1.Activate
End Sub
Re: บันทึกข้อมูล
Posted: Fri Nov 17, 2017 6:31 pm
by snasui
Benmore wrote:ไฟล์แนบที่แก้ไขโค้ดแล้วรันไม่ได้ตามที่ต้องการค่ะ
Code: Select all
'Save
Private Sub btsave_Click()
On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "¡ÃسҡÃÍ¡¢éÍÁÙÅãËé¤Ãº¶éǹ"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
If VBA.Left(ct.Name, 4) = "Opt" And ct.Value = True Then
Debug.Print ct.Name
Cells(emptyRow, 8).Value = ct.Caption
Exit For
End If
Next ct
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
'emptyRow = WorksheetFunction.Count("A3:A10000") + 1
If emptyRow = 0 Then
emptyRow = 2
Else
emptyRow = emptyRow + 2
Sheet9.Activate
strTb1 = Split(TextBox1.Text, vbCrLf)
strTb3 = TextBox3.Text & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = Split(strTb3, vbCrLf)
Cells(emptyRow, 1).Value = VBA.Mid(strTb1(0), InStr(strTb1(0), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 2).Value = VBA.Mid(strTb1(1), InStr(strTb1(1), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 3).Value = VBA.Mid(strTb1(2), InStr(strTb1(2), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 4).Value = VBA.Mid(strTb1(3), InStr(strTb1(3), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 6).Value = strTb3(0) & "," & strTb3(1) & "," & strTb3(2) & vbCrLf & strTb3(3) & "," & strTb3(4) & "," & strTb3(5) 'TextBox3.Value
'Cells(emptyRow, 7).Value = OptionButton
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "ºÑ¹·Ö¡¢éÍÁÙÅàÃÕºÃéÍÂáÅéÇ"
Unload Me
UserForm1.Show
End If
Sheet1.Activate
End Sub

Code ที่แนบมาไม่เป็นไปตามที่ผมตอบไปตามโพสต์ #26 ตามที่ยกมาด้านล่างนี้ครับ
snasui wrote:
เรียงลำดับ Code ไม่ถูกต้องครับ
ตัวอย่างการเรียงลำดับ Code ควรจะเป็นด้านล่าง
Code: Select all
Private Sub btsave_Click()
On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "¡ÃسҡÃÍ¡¢éÍÁÙÅãËé¤Ãº¶éǹ"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
Sheet9.Activate
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
For Each ct In Me.Frame2.Controls
If VBA.Left(ct.Name, 3) = "Opt" And ct.Value = True Then
Cells(emptyRow, 7).Value = ct.Caption
Exit For
End If
Next ct
For Each ct In Me.Frame5.Controls
'Other code
สังเกตตัวแปรที่จะกำหนดค่าบรรทัดจะต้องกำหนดเอาไว้ก่อนที่จะนำข้อมูลไปวาง
ควรจะเอา On Error Resume Next ออกเสียก่อนแล้วทำการ Debug จะได้ทราบได้เองเบื้องต้นว่าผิดพลาดหรือไม่อย่างไรครับ
Re: บันทึกข้อมูล
Posted: Mon Nov 20, 2017 9:56 am
by Benmore
รบกวนตรวจสอบโค้ดให้หน่อยค่ะ พอดีเวลาค้นหาข้อมูลด้วยรหัสแล้วมันค้นหาไม่ได้ค่ะ
Code: Select all
Private Sub btsearch1_Click()
On Error Resume Next
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim chkDate As Date
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet9.Activate
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
If Err.Number = 91 Then
TextBox1.RowSource = "txtsearch1.Text"
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
TextBox11.Value = ""
TextBox16.Value = ""
TextBox17.Value = ""
TextBox12.Value = ""
TextBox13.Value = ""
'MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
TextBox9.Value = Cells(nRow, 3)
TextBox10.Value = Cells(nRow, 4)
TextBox11.Value = Cells(nRow, 6)
TextBox16.Value = Cells(nRow, 7)
TextBox17.Value = Cells(nRow, 8)
TextBox12.Value = Cells(nRow, 9)
TextBox13.Value = Cells(nRow, 10)
Exit Sub
Else
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
Sheet1.Activate
End Sub
Uniform_EGAS(Ex).zip
Re: บันทึกข้อมูล
Posted: Mon Nov 20, 2017 4:14 pm
by Benmore
คำถามด้านบนทำได้แล้วรบกวนดูโค้ดบันทึกข้อมูลหน่อยค่ะ
เมื่อเลือกข้อมูลใน optionbutton แล้วข้อมูลไม่ถูกบันทึกลงในชีท 9 "การเบิก" ค่ะ
Code: Select all
Private Sub btsave_Click()
'On Error Resume Next
If TextBox1 = "" Or TextBox3 = "" Then
MsgBox "กรุณากรอกข้อมูลให้ครบถ้วน"
Exit Sub
End If
Dim emptyRow As Integer
Dim ct As Object
Dim strTb1 As Variant
Dim strTb3 As Variant
opt = OptionButton1.Value Or OptionButton2.Value
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
'emptyRow = WorksheetFunction.Count("A3:A10000") + 1
If emptyRow = 0 Then
emptyRow = 2
Else
emptyRow = emptyRow + 2
Sheet9.Activate
strTb1 = Split(TextBox1.Text, vbCrLf)
strTb3 = TextBox3.Text & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = strTb3 & vbCrLf
strTb3 = Split(strTb3, vbCrLf)
Cells(emptyRow, 1).Value = VBA.Mid(strTb1(0), InStr(strTb1(0), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 2).Value = VBA.Mid(strTb1(1), InStr(strTb1(1), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 3).Value = VBA.Mid(strTb1(2), InStr(strTb1(2), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 4).Value = VBA.Mid(strTb1(3), InStr(strTb1(3), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 6).Value = strTb3(0) & "," & strTb3(1) & "," & strTb3(2) 'TextBox3.Value
Cells(emptyRow, 7).Value = strTb3(3) & "," & strTb3(4) & "," & strTb3(5)
Cells(emptyRow, 8).Value = strTb3(6) & "," & strTb3(7)
If OptionButton1.Value = True Then
Cells(CurrentRow, 10).Value = "มารับแล้ว"
ElseIf OptionButton2.Value = True Then
Cells(CurrentRow, 10).Value = "ไม่ได้มารับ"
ElseIf OptionButton3.Value = True Then
Cells(CurrentRow, 10).Value = TextBox15.Value
End If
If OptionButton4.Value = True Then
Cells(CurrentRow, 9).Value = "ชุดหดและเก่าตามสภาพ"
ElseIf OptionButton5.Value = True Then
Cells(CurrentRow, 9).Value = "ชุดเปื่อยขาด เนื่องจากการซัก"
ElseIf OptionButton6.Value = True Then
Cells(CurrentRow, 9).Value = "ชุดขาดตามรอยตะเข็บ"
ElseIf OptionButton7.Value = True Then
Cells(CurrentRow, 9).Value = "เดินทางไปต่างจังหวัด/ต่างประเทศ"
ElseIf OptionButton8.Value = True Then
Cells(CurrentRow, 9).Value = TextBox2.Value
End If
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "บันทึกข้อมูลเรียบร้อยแล้ว"
Unload Me
UserForm1.Show
End If
'Sheet1.Activate
End Sub
Re: บันทึกข้อมูล
Posted: Tue Nov 21, 2017 7:38 pm
by snasui

โปรแกรมฟ้อง Error ตรงไหน อย่างไร ช่วย Debug และแจ้งมาด้วยครับ
จากที่ดู Code ไม่ทราบว่าตัวแปร CurrentRow มีค่าเท่ากับค่าใด กำหนดค่าไว้ที่บรรทัดใดครับ
ผมเข้าใจว่าเป็นค่าเดียวกับตัวแปร emptyRow หากเป็นค่าเดียวกันให้ใช้ตัวแปร emptyRow ได้เลยครับ
Re: บันทึกข้อมูล
Posted: Wed Nov 22, 2017 8:49 am
by Benmore
Re: บันทึกข้อมูล
Posted: Fri Nov 24, 2017 8:47 am
by Benmore
Re: บันทึกข้อมูล
Posted: Sun Nov 26, 2017 12:33 pm
by snasui

การให้กรอกวันที่เองมีความยุ่งยากในการตรวจสอบเพราะสามารถจะคีย์เป็นตัวเลขวันแล้วตามด้วยเดือนเป็นข้อความ หากแยกช่องเป็น วัน เดือน ปี แล้วเช็คแต่ละช่องว่าเป็นไปตามที่กำหนดหรือไม่จะง่ายกว่าครับ
ฟอรัมนี้ไม่อนุญาตให้ดันกระทู้โดยไม่มีการโพสต์ข้อความอื่นใดเพิ่มเติม การตอบปัญหาขึ้นอยู่กับเวลาว่างของผู้ตอบ ขออภัยในความไม่สะดวกครับ
Re: บันทึกข้อมูล
Posted: Mon Nov 27, 2017 4:51 pm
by Benmore
ตรงนี้แก้ไขได้แล้วค่ะ รบกวนดูโค้ด Delete ให้ด้วยค่ะ
ถ้าต้องการลบข้อมูลที่ค้นหามาต้องแก้ไขโค้ดเพิ่มตรงไหนบ้างค่ะ
โค้ดค้นหาข้อมูลจากชีทการเบิก แล้วก็ให้ลบออกจากชีทการเบิกค่ะ

Userform1
Code: Select all
Private Sub CommandButton6_Click()
Dim id As String
Dim rowselect As String
If TextBox7.Text = "" Then
MsgBox ("กรุณาเลือกข้อมูล")
Else
id = TextBox7.Text
rowselect = WorksheetFunction.Match(id, Sheet6.Range("A1:A300"), 0)
Rows(rowselect).Select
Rows(rowselect).EntireRow.Delete
End If
Unload Me
UserForm1.Show
End Sub
Re: บันทึกข้อมูล
Posted: Wed Nov 29, 2017 6:58 am
by snasui

ลบชีตการเบิก เปลี่ยน Sheet6 เป็น Sheet9 ครับ
rowselect = WorksheetFunction.Match(id, Sheet9.Range("A1:A300"), 0)