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]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)ลองแก้โค้ดเองได้แล้วค่ะ แต่คือติดปัญหาตรงที่ไม่ต้องการเอาหัวข้อไปแสดงที่ชีทต้องแก้ตรงไหนค่ะแล้วก็เอาข้อมูลใน textbox3 ไปแสดงในชีท excel ไม่ได้ค่ะ (Userform1)snasui wrote: ต้องกำหนดหัวข้อเข้าไปเองหรืออ้างอิงจากที่มีอยู่แล้ว ลองเขียนมาเองก่อน ติดตรงไหนแล้วค่อยถามกันต่อครับ
Code: Select all
'ค้นหา
Private Sub btsearch_Click()
Dim txt As String
On Error Resume Next
'Err.Clear
Dim nRow As String
nRow = Sheet6.Columns(1).Find(txtsearch.Text).Row
If Err.Number = 91 Then
TextBox1.RowSource = "txtsearch.Text"
MsgBox "ไม่มีข้อมูล"
End If
txt = "Emp_ID :" & Cells(nRow, 1) & "Name :" & vbCrLf & Cells(nRow, 2) & "Section :" & vbCrLf & Cells(nRow, 8) _
& "Uniform_No :" & vbCrLf & Cells(nRow, 13)
TextBox1.Value = txt
End Sub
Code: Select all
'Save
Private Sub btsave_Click()
Dim emptyRow As Integer
Dim strTb1 As Variant
Dim strTb2 As Variant
emptyRow = WorksheetFunction.Count("B3:A1000") + 1
If emptyRow = 0 Then
emptyRow = 2
Else
emptyRow = emptyRow + 2
Sheet9.Activate
strTb1 = Split(TextBox1.Text, vbCrLf)
strTb2 = Split(TextBox3.Text, vbCrLf)
Cells(emptyRow, 1).Value = strTb1(0) 'TextBox1.Value
Cells(emptyRow, 2).Value = strTb1(1) 'TextBox1.Value
Cells(emptyRow, 3).Value = strTb1(2) 'TextBox1.Value
Cells(emptyRow, 4).Value = strTb1(3) 'TextBox1.Value
Cells(emptyRow, 5).Value = comday.Value & "/" & commonth.Value & "/" & comyear.Value
Cells(emptyRow, 6).Value = strTb2(6) 'TextBox3.Value
MsgBox "บันทึกข้อมูลเรียบร้อย"
End If
End Sub
txt
ตามด้านล่างครับCode: Select all
'...Other code
txt = "Emp_ID :" & Cells(nRow, 1) & vbCrLf & _
"Name :" & Cells(nRow, 2) & vbCrLf & _
"Section :" & Cells(nRow, 8) & vbCrLf & _
"Uniform_No :" & Cells(nRow, 13)
'...Other code
txt
Code: Select all
'...Other code
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
'...Other code
Code: Select all
'Search
Private Sub btsearch_Click()
On Error Resume Next
Dim nRow As String
nRow = Sheet6.Columns(1).Find(txtsearch.Text).Row
If Err.Number = 91 Then
ComboBox1.Value = " "
TextBox1.Value = " "
TextBox2.Value = " "
TextBox3.Value = " "
TextBox4.Value = " "
ComboBox2.Value = " "
TextBox5.Value = " "
ComboBox3.Value = " "
TextBox6.Value = " "
ComboBox4.Value = " "
TextBox7.Value = " "
ComboBox5.Value = " "
ComboBox6.Value = " "
MsgBox "ไม่มีข้อมูล"
End If
ComboBox1.Value = Cells(nRow, 1)
TextBox1.Value = Cells(nRow, 2)
TextBox2.Value = Cells(nRow, 3)
TextBox3.Value = Cells(nRow, 4)
TextBox4.Value = Cells(nRow, 5)
ComboBox2.Value = Cells(nRow, 6)
TextBox5.Value = Cells(nRow, 7)
ComboBox3.Value = Cells(nRow, 8)
TextBox6.Value = Cells(nRow, 9)
ComboBox4.Value = Cells(nRow, 10)
TextBox7.Value = Cells(nRow, 11)
ComboBox5.Value = Cells(nRow, 12)
ComboBox6.Value = Cells(nRow, 13)
End Sub
Code: Select all
'...Other code
On Error Resume Next
Dim nRow As String
If Not IsNumeric(vba.right(txtsearch.Text,3)) Then
MsgBox "กรุณาใสข้อมูลเป็นตัวเลข"
Exit Sub
End If
'...Other code
Code: Select all
Private Sub btsearch_Click()
On Error Resume Next
Dim nRow As String
If Not IsNumeric(VBA.Right(txtsearch.Text, 3)) Then
MsgBox "¡ÃسÒãÊ¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
nRow = Sheet6.Columns(1).Find(txtsearch.Text).Row
If Err.Number = 91 Then
ComboBox1.Value = " "
TextBox1.Value = " "
TextBox2.Value = " "
TextBox3.Value = " "
TextBox4.Value = " "
ComboBox2.Value = " "
TextBox5.Value = " "
ComboBox3.Value = " "
TextBox6.Value = " "
ComboBox4.Value = " "
TextBox7.Value = " "
ComboBox5.Value = " "
ComboBox6.Value = " "
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
ComboBox1.Value = Cells(nRow, 1)
TextBox1.Value = Cells(nRow, 2)
TextBox2.Value = Cells(nRow, 3)
TextBox3.Value = Cells(nRow, 4)
TextBox4.Value = Cells(nRow, 5)
ComboBox2.Value = Cells(nRow, 6)
TextBox5.Value = Cells(nRow, 7)
ComboBox3.Value = Cells(nRow, 8)
TextBox6.Value = Cells(nRow, 9)
ComboBox4.Value = Cells(nRow, 10)
TextBox7.Value = Cells(nRow, 11)
ComboBox5.Value = Cells(nRow, 12)
ComboBox6.Value = Cells(nRow, 13)
End Sub
Code: Select all
for each r in sheet6.columns(1).specialcells(xlcelltypeconstants)
if right(r.value,3) = right(txtsearch.text,3) then
nRow = r.row
exit for
end if
end if
ลองปรับแล้วก็ยังไม่ได้ค่ะ ต้องปรับแบบไหนค่ะlogic wrote:เท่าที่อ่านดูตรง nRow ผมว่ามันต้อง loop หาแทนการ search นะครับ
ลองเอาไปปรับดูครับCode: Select all
for each r in sheet6.columns(1).specialcells(xlcelltypeconstants) if right(r.value,3) = right(txtsearch.text,3) then nRow = r.row exit for end if end if
snasui wrote: แนบไฟล์ล่าสุดที่ได้ปรับ Code ตามที่คุณ logic แจ้งไปมาด้วย จะได้ช่วยดูต่อไปจากนั้นครับ
Code: Select all
Private Sub btsearch_Click()
Dim found As Boolean
On Error Resume Next
Dim nRow As String
For Each r In Sheet6.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch.Text, 3) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch.Text, 3)) Then
MsgBox "¡ÃسÒãÊ¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
If Err.Number = 91 Then
ComboBox1.Value = " "
TextBox1.Value = " "
TextBox2.Value = " "
TextBox3.Value = " "
TextBox4.Value = " "
ComboBox2.Value = " "
TextBox5.Value = " "
ComboBox3.Value = " "
TextBox6.Value = " "
ComboBox4.Value = " "
TextBox7.Value = " "
ComboBox5.Value = " "
ComboBox6.Value = " "
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
ComboBox1.Value = Cells(nRow, 1)
TextBox1.Value = Cells(nRow, 2)
TextBox2.Value = Cells(nRow, 3)
TextBox3.Value = Cells(nRow, 4)
TextBox4.Value = Cells(nRow, 5)
ComboBox2.Value = Cells(nRow, 6)
TextBox5.Value = Cells(nRow, 7)
ComboBox3.Value = Cells(nRow, 8)
TextBox6.Value = Cells(nRow, 9)
ComboBox4.Value = Cells(nRow, 10)
TextBox7.Value = Cells(nRow, 11)
ComboBox5.Value = Cells(nRow, 12)
ComboBox6.Value = Cells(nRow, 13)
Exit Sub
Else
MsgBox "Not found."
End If
End Sub
Dim r As Range
เป็นต้นครับค้นหาแก้ไขได้แล้วค่ะ แต่ติดตรงส่วนของการเลือกข้อมูลใหม่ให้ข้อมูลที่เลือกก่อนหน้าหายไป และก็ช่องจำนวนค่ะ พอพิมจำนวนสองตัวก็จะขึ้นข้อมูลสองครั้งค่ะ เช่น ถ้าพิม 1 จะขึ้น "จำนวน 1" ถ้าเพิ่ม 1 อีกตัวก็จะขึ้น "จำนวน 1" "จำนวน 11" แบบนี้ค่ะsnasui wrote: กรณี Error ช่วยจับภาพหรือแจ้ง Error มาดูกัน หากบรรทัดบนสุดมีคำว่า Option Explicit จะต้องประกาศตัวแปรก่อนที่จะใช้ตัวแปรนั้นเสมอ เช่นDim r As Range
เป็นต้นครับ
ในส่วนของการเลือกข้อมูลใหม่ให้ช่องจำนวนหายไป ขึ้นอยู่กับ Code ในช่องที่เราเลือกว่าเขียนไว้อย่างไร ช่วยโพสต์หรือแจ้งไว้ด้วยจะได้เข้าถึงข้อมูลได้โดยไวครับ
Code: Select all
Private Sub ComboBox1_Change()
If Combobox1.Value = "เสื้อแขนสั้น(Short Shirt)" Then
Combobox2.RowSource = "DATA!G2:G8"
End If
If Combobox1.Value = "เสื้อแขนยาว(Long Shirt)" Then
Combobox2.RowSource = "DATA!G2:G8"
End If
If Combobox1.Value = "กางเกง(Trousere)" Then
Combobox2.RowSource = "DATA!H2:H24"
Else
Combobox2.Value = ""
End If
If Combobox1.Value <> "" Then
TextBox3.Value = TextBox3.Value & vbCrLf & "" & Combobox1.Value
End If
End Sub
Private Sub ComboBox2_Change()
If Combobox2.Value <> "" Then
TextBox3.Value = TextBox3.Value & vbCrLf & " ไซส์ " & Combobox2.Value
End If
End Sub
'จำนวน
Private Sub TextBox4_Change()
On Error Resume Next
If TextBox4.Value <> "" Then
TextBox3.Value = TextBox3.Value & vbCrLf & " จำนวน " & TextBox4.Value
End If
End Sub
Code: Select all
'Other code
If Combobox1.Value <> "" Then
TextBox3.Value = ""
TextBox3.Value = TextBox3.Value & vbCrLf & "" & Combobox1.Value
End If
'Other code
อันนี้คือโค้ดตรงส่วนไหนค่ะsnasui wrote: ตัวอย่าง Code ครับ
Code: Select all
'Other code If Combobox1.Value <> "" Then TextBox3.Value = "" TextBox3.Value = TextBox3.Value & vbCrLf & "" & Combobox1.Value End If 'Other code
- พอดีอยากทราบว่าโค้ดตรงส่วนของการเลือกข้อมูลใหม่ให้ข้อมูลที่เลือกก่อนหน้าหายไป ต้องเพิ่มตรงไหนค่ะsnasui wrote: โค้ดนั้นอยู่ใน Procedure ที่คุณ Benmore โพสต์มาครับ ตรวจสอบว่าบรรทัดใดเป็นการกำหนดค่าให้กับ TextBox3 ผมเพิ่มเข้าไปแค่บรรทัดเดียว น่าจะสังเกตได้ไม่ยากครับ
ลองเอาโค้ดไปแก้ไขแล้วค่ะแต่คือข้อมูลใน combobox หายไปได้แต่ข้อมูลใน textbox ก็หายไปด้วย คืออยากให้แค่ข้อมูลใน combobox หายไปอย่างเดียวค่ะsnasui wrote: ผมตอบไปแล้วตามโพสต์ด้านบน ลองทดสอบกับทุก Code ที่เกียวข้องกับปัญหานี้ แล้วสังเกตดูผล
โปรดทราบว่าผมจะตอบด้วย Code ที่เกี่ยวข้องกับสิ่งที่ถามเสมอครับ กรณีทดสอบแล้วยังไม่ได้คำตอบให้แนบไฟล์ที่ปรับ Code เป็นล่าสุดมาแล้ว