Page 2 of 5

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 10, 2017 6:08 am
by snasui
:D ต้องกำหนดหัวข้อเข้าไปเองหรืออ้างอิงจากที่มีอยู่แล้ว ลองเขียนมาเองก่อน ติดตรงไหนแล้วค่อยถามกันต่อครับ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 10, 2017 10:06 am
by Benmore
snasui wrote::D ต้องกำหนดหัวข้อเข้าไปเองหรืออ้างอิงจากที่มีอยู่แล้ว ลองเขียนมาเองก่อน ติดตรงไหนแล้วค่อยถามกันต่อครับ
ลองแก้โค้ดเองได้แล้วค่ะ แต่คือติดปัญหาตรงที่ไม่ต้องการเอาหัวข้อไปแสดงที่ชีทต้องแก้ตรงไหนค่ะแล้วก็เอาข้อมูลใน textbox3 ไปแสดงในชีท excel ไม่ได้ค่ะ (Userform1)

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 save

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
ไฟล์แนบค่ะ
Uniform_EGAS(Ex).xlsm

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 10, 2017 6:32 pm
by snasui
:D ตัวอย่างการกำหนดตัวแปร 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
ตัวอย่างการ Code บางส่วนเมื่อกดปุ่ม Save ซึ่งเป็น 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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Thu Oct 12, 2017 3:45 pm
by Benmore
สอบถามเรื่องการค้นหาข้อมูลเพิ่มค่ะ อยากให้ค้นหาข้อมูลได้เฉพาะตัวเลข 3 ตัวหลังของรหัสพนักงานค่ะ ถ้าใส่ข้อมูลเป็นตัวหนังสือก็ให้แสดง msg ว่า "กรุณาใสข้อมูลเป็นตัวเลข" ใน user form 11

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Thu Oct 12, 2017 6:34 pm
by snasui
:D ตัวอย่าง Code ครับ

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Mon Oct 16, 2017 9:16 am
by Benmore
ลองเอาโค้ดไปปรับแล้วก็ยังค้นหาเจอทั้งที่ไม่มีรหัสนี้อยู่ค่ะ

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Mon Oct 16, 2017 1:48 pm
by logic
เท่าที่อ่านดูตรง 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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Mon Oct 16, 2017 3:17 pm
by Benmore
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
ลองปรับแล้วก็ยังไม่ได้ค่ะ ต้องปรับแบบไหนค่ะ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Mon Oct 16, 2017 6:34 pm
by snasui
:D แนบไฟล์ล่าสุดที่ได้ปรับ Code ตามที่คุณ logic แจ้งไปมาด้วย จะได้ช่วยดูต่อไปจากนั้นครับ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 17, 2017 8:33 am
by Benmore
Uniform_EGAS(Ex).xlsm
snasui wrote::D แนบไฟล์ล่าสุดที่ได้ปรับ Code ตามที่คุณ logic แจ้งไปมาด้วย จะได้ช่วยดูต่อไปจากนั้นครับ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 17, 2017 8:33 pm
by snasui
:D ตัวอย่าง Code ครับ

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Wed Oct 18, 2017 10:09 am
by Benmore
- นำไปแก้ไขได้แล้วค่ะ แต่พอเอาโค้ดไปใส่ในฟอร์มอื่นก็ error ค่ะ เพราะอะไรทั้งๆที่เป็นโค้ดเดียวกัน ที่ userform1
error1.png
- ขอบสอบถามเพิ่มเติมนะค่ะ ทำอย่างไรให้ข้อมูลจำนวนที่เรากรอกขึ้นแค่อันเดียว
แล้วพอเราเลือกข้อมูลอันใหม่ทำให้ข้อมูลอันเก่าที่เคยเลือกหายไปต้องแก้ตรงไหนค่ะ
error.png

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Wed Oct 18, 2017 10:13 pm
by snasui
:D กรณี Error ช่วยจับภาพหรือแจ้ง Error มาดูกัน หากบรรทัดบนสุดมีคำว่า Option Explicit จะต้องประกาศตัวแปรก่อนที่จะใช้ตัวแปรนั้นเสมอ เช่น Dim r As Range เป็นต้นครับ

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Thu Oct 19, 2017 9:04 am
by Benmore
snasui wrote::D กรณี Error ช่วยจับภาพหรือแจ้ง Error มาดูกัน หากบรรทัดบนสุดมีคำว่า Option Explicit จะต้องประกาศตัวแปรก่อนที่จะใช้ตัวแปรนั้นเสมอ เช่น Dim r As Range เป็นต้นครับ

ในส่วนของการเลือกข้อมูลใหม่ให้ช่องจำนวนหายไป ขึ้นอยู่กับ Code ในช่องที่เราเลือกว่าเขียนไว้อย่างไร ช่วยโพสต์หรือแจ้งไว้ด้วยจะได้เข้าถึงข้อมูลได้โดยไวครับ
ค้นหาแก้ไขได้แล้วค่ะ แต่ติดตรงส่วนของการเลือกข้อมูลใหม่ให้ข้อมูลที่เลือกก่อนหน้าหายไป และก็ช่องจำนวนค่ะ พอพิมจำนวนสองตัวก็จะขึ้นข้อมูลสองครั้งค่ะ เช่น ถ้าพิม 1 จะขึ้น "จำนวน 1" ถ้าเพิ่ม 1 อีกตัวก็จะขึ้น "จำนวน 1" "จำนวน 11" แบบนี้ค่ะ

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

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Thu Oct 19, 2017 5:40 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

'Other code
If Combobox1.Value <> "" Then
    TextBox3.Value = ""
    TextBox3.Value = TextBox3.Value & vbCrLf & "" & Combobox1.Value
End If
'Other code

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 24, 2017 10:34 am
by Benmore
snasui wrote::D ตัวอย่าง Code ครับ

Code: Select all

'Other code
If Combobox1.Value <> "" Then
    TextBox3.Value = ""
    TextBox3.Value = TextBox3.Value & vbCrLf & "" & Combobox1.Value
End If
'Other code
อันนี้คือโค้ดตรงส่วนไหนค่ะ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 24, 2017 10:41 am
by snasui
:D โค้ดนั้นอยู่ใน Procedure ที่คุณ Benmore โพสต์มาครับ ตรวจสอบว่าบรรทัดใดเป็นการกำหนดค่าให้กับ TextBox3 ผมเพิ่มเข้าไปแค่บรรทัดเดียว น่าจะสังเกตได้ไม่ยากครับ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 24, 2017 11:58 am
by Benmore
snasui wrote::D โค้ดนั้นอยู่ใน Procedure ที่คุณ Benmore โพสต์มาครับ ตรวจสอบว่าบรรทัดใดเป็นการกำหนดค่าให้กับ TextBox3 ผมเพิ่มเข้าไปแค่บรรทัดเดียว น่าจะสังเกตได้ไม่ยากครับ
- พอดีอยากทราบว่าโค้ดตรงส่วนของการเลือกข้อมูลใหม่ให้ข้อมูลที่เลือกก่อนหน้าหายไป ต้องเพิ่มตรงไหนค่ะ
- ช่องจำนวน พอพิมจำนวนสองตัวก็จะขึ้นข้อมูลสองครั้งค่ะ เช่น ถ้าพิม 1 จะขึ้น "จำนวน 1" ถ้าเพิ่ม 1 อีกตัวก็จะขึ้น "จำนวน 1" "จำนวน 11" แบบนี้ค่ะ

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 24, 2017 1:26 pm
by snasui
:D ผมตอบไปแล้วตามโพสต์ด้านบน ลองทดสอบกับทุก Code ที่เกียวข้องกับปัญหานี้ แล้วสังเกตดูผล

โปรดทราบว่าผมจะตอบด้วย Code ที่เกี่ยวข้องกับสิ่งที่ถามเสมอครับ กรณีทดสอบแล้วยังไม่ได้คำตอบให้แนบไฟล์ที่ปรับ Code เป็นล่าสุดมาแล้ว

Re: ค้นหาข้อมูลแล้วให้ข้อมูลโชว์ใน Textbox

Posted: Tue Oct 24, 2017 2:14 pm
by Benmore
snasui wrote::D ผมตอบไปแล้วตามโพสต์ด้านบน ลองทดสอบกับทุก Code ที่เกียวข้องกับปัญหานี้ แล้วสังเกตดูผล

โปรดทราบว่าผมจะตอบด้วย Code ที่เกี่ยวข้องกับสิ่งที่ถามเสมอครับ กรณีทดสอบแล้วยังไม่ได้คำตอบให้แนบไฟล์ที่ปรับ Code เป็นล่าสุดมาแล้ว
ลองเอาโค้ดไปแก้ไขแล้วค่ะแต่คือข้อมูลใน combobox หายไปได้แต่ข้อมูลใน textbox ก็หายไปด้วย คืออยากให้แค่ข้อมูลใน combobox หายไปอย่างเดียวค่ะ