EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
'Save
Private Sub btsave_Click()
On Error Resume Next
Dim emptyRow As Integer
Dim strTb1 As Variant
Dim strTb3 As Variant
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 = VBA.Mid(strTb3(6), InStr(strTb3(6), ":") + 1) & vbCrLf & VBA.Mid(strTb3(7), InStr(strTb3(7), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "บันทึกข้อมูลเรียบร้อยแล้ว"
Unload Me
Sheet1.Activate
End If
End Sub
รบกวนขอผู้รู้ช่วยหน่อยค่ะBenmore wrote:ขอสอบถามหน่อยค่ะ พอดีตอนนี้ติดปัญหาในการบันทึกข้อมูลค่ะ
เมื่อบันทึกข้อมูลลงไป ข้อมูลจะถูกบันทึกลงในชีท excel แทนที่กันไปเรื่อยๆไม่บันทึกลงมาเรื่อยๆค่ะ ต้องแก้ตรงไหนค่ะCode: Select all
'Save Private Sub btsave_Click() On Error Resume Next Dim emptyRow As Integer Dim strTb1 As Variant Dim strTb3 As Variant 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 = VBA.Mid(strTb3(6), InStr(strTb3(6), ":") + 1) & vbCrLf & VBA.Mid(strTb3(7), InStr(strTb3(7), ":") + 1) 'TextBox1.Value Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value MsgBox "บันทึกข้อมูลเรียบร้อยแล้ว" Unload Me Sheet1.Activate End If End Sub
emptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
ตรงบันทึกทำได้แล้วขอบคุณมากค่ะ (ติดตรงนี้มานานมากกกกกกก55555)snasui wrote: ตัวอย่างการปรับ Code ครับemptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
รบกวนขอผู้รู้ช่วยหน่อยค่ะBenmore wrote:ตรงบันทึกทำได้แล้วขอบคุณมากค่ะ (ติดตรงนี้มานานมากกกกกกก55555)snasui wrote: ตัวอย่างการปรับ Code ครับemptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
ขอสอบถามเพิ่มเติ่มค่ะ
ถ้าต้องการให้เลือกข้อมูลในวงกลมแล้วไม่ให้ข้อมูลแสดงใน textbox3 แต่เมื่อบันทึกข้อมูลลงไปให้ข้อมูลถูกบันทึกในชีทตามที่กำหนดต้องแก้โค้ดเพิ่มตรงไหนบ้างค่ะ
รูป.png
พอดีว่ากระทู้อื่นทำได้หมดแล้วค่ะ เลยรายงานผลให้ผู้ที่ตอบให้ค่ะว่าทำได้แล้วค่ะkio2002 wrote:กระทู้ของคุณ benmore เยอะมากครับ ผมตามไม่ทันเลย
Project ของคุณ benmore ที่กำลังทำอยู่ ถือว่าค่อนข้างซับซ้อนครับ ต้องมีพื้นฐานและความเข้าใจและชำนาญเกี่ยวกับ VBA เป็นอย่างมาก
ก่อนหน้านี้ ผมมี Project ยากๆที่ผมเคยลองดันตัวเองแล้ว1ครั้ง แต่ไปไม่รอด เพราะยังมีความเข้าใจเกี่ยวกับ VBA น้อย
ขั้นตอนการเลือกแล้วไม่เพิ่มใน Textbox3 ไม่ควรจะเป็นปัญหาเพราะไปยกเลิก Change Event เมื่อเลือกไม่ให้ดำเนินการใด ๆ ได้ ปัญหาน่าจะอยู่ที่การบันทักข้อมูลลงในชีตแล้วให้ข้อมูลใน Control เหล่าสนั้นนำไปวางด้วย ได้เขียนการทำงานส่วนนี้เพิ่มเข้าไปแล้วหรือไม่ ติดขัดบรรทัดใด หากยังไม่เขียน กุณาเขียนมาเองก่อน ถามเฉพาะที่ติดปัญหา แจ้งตัวอย่างผลลัพธ์ที่ต้องการเอาไว้ด้วยว่าหากบันทึกถูกต้องจะแสดงรายการที่ชีตใด เซลล์ใด ด้วยค่าเท่าใด จะได้สะดวกในการทดสอบครับBenmore wrote:ตรงบันทึกทำได้แล้วขอบคุณมากค่ะ (ติดตรงนี้มานานมากกกกกกก55555)snasui wrote: ตัวอย่างการปรับ Code ครับemptyRow = WorksheetFunction.CountA(Sheet9.Range("A3:A10000")) + 1
ขอสอบถามเพิ่มเติ่มค่ะ
ถ้าต้องการให้เลือกข้อมูลในวงกลมแล้วไม่ให้ข้อมูลแสดงใน textbox3 แต่เมื่อบันทึกข้อมูลลงไปให้ข้อมูลถูกบันทึกในชีทตามที่กำหนดต้องแก้โค้ดเพิ่มตรงไหนบ้างค่ะ
รูป.png
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 strTb1 As Variant
Dim strTb3 As Variant
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, 7).Value = VBA.Mid(strTb3(6), InStr(strTb3(6), ":") + 1) & vbCrLf & VBA.Mid(strTb3(7), InStr(strTb3(7), ":") + 1) 'TextBox1.Value
'Cells(emptyRow, 8).Value = VBA.Mid(strTb3(8), InStr(strTb3(8), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "บันทึกข้อมูลเรียบร้อย"
Unload Me
UserForm1.Show
End If
Sheet1.Activate
End Sub
Code: Select all
'Other code
dim ct as object
'Other code
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
snasui wrote: ตัวอย่างการ Loop Option Button ทีมีการเลือกแล้ววางลงในชีตครับ
ปรับให้ใช้ได้กับ Option Button ตัวสุดท้ายที่เป็นตัวเลือกอื่น ๆ ที่ไม่ได้นำ Caption ของ Option Button มาใช้แต่เป็นการตำค่าใน TextBox ที่ผู้ใช้งานคีย์เข้าไปเองมาใช้แทนครับCode: Select all
'Other code dim ct as object 'Other code 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
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, 7).Value = VBA.Mid(strTb3(6), InStr(strTb3(6), ":") + 1) & vbCrLf & VBA.Mid(strTb3(7), InStr(strTb3(7), ":") + 1) 'TextBox1.Value
'Cells(emptyRow, 8).Value = VBA.Mid(strTb3(8), InStr(strTb3(8), ":") + 1) 'TextBox1.Value
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
MsgBox "บันทึกข้อมูลเรียบร้อยแล้ว"
Unload Me
UserForm1.Show
End If
Sheet1.Activate
End Sub{code]
ปรับโค้ดแล้วก็ยังไม่ได้ค้ะ ต้องปรับตรงไหนเพิ่มค่ะ
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
ให้เลือกข้อมูลใน option button แล้วข้อมูลที่เราเลือกก็จะถูกบันทึกลงในชีทการเบิกในคอลลัมตามที่เราต้องการค่ะsnasui wrote: กรุณาอ่านที่ผมโพสต์และแจ้งมาตามนั้นให้ครบถ้วนทุกข้อความที่ผมเขียนแจ้งไป โดยเขียนมาในช่องความเห็นนี้ด้วยครับ
snasui wrote: กรุณาอ่านที่ผมโพสต์และแจ้งมาตามนั้นให้ครบถ้วนทุกข้อความที่ผมเขียนแจ้งไป โดยเขียนมาในช่องความเห็นนี้ด้วยครับ
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
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