ค่าใน Combobox แสดงใน Textbox แล้วสามารถบันทึกลงใน sheet
Posted: Fri Dec 21, 2018 10:59 am
ติดปัญหา Code error ค่ะ เขียน Code แล้ว ให้ข้อมูลใน sheet Other แสดงค่าใน ComboBox แต่ไม่สามารถบันทึกค่าลงใน sheet Database ได้ค่ะ
ฟอรัม Excel, VBA และอื่นๆ ของคนไทยเพื่อประโยชน์ของทุกคนในจักรวาล (Forum Excel, VBA and others of Thai people for everyone in the universe.)
https://snasui.com/
Code: Select all
Private Sub ComboBox1_Change()
'Select Case ComboBox1.ListIndex
'Case 0
'a = "Other!A2:A100"
'ComboBox1.RowSource = a
'End Select
TextBox7.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A:E"), 2, False)
TextBox8.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A:E"), 3, False)
TextBox9.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A:E"), 4, False)
TextBox10.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A:E"), 5, False)
'TextBox12.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 2, False)
'TextBox13.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 3, False)
'TextBox14.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 4, False)
'TextBox15.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 5, False)
'TextBox17.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A4:E4"), 2, False)
'TextBox18.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A4:E4"), 3, False)
'TextBox19.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A4:E4"), 4, False)
'TextBox20.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A4:E4"), 5, False)
End Sub
Private Sub CommandButton1_Click()
Dim irow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")
'find first empty row in database
irow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'copy the data to the database
ws.Cells(irow, 1).Value = Me.TextBox1.Value
ws.Cells(irow, 2).Value = Me.TextBox2.Value
ws.Cells(irow, 3).Value = Me.TextBox4.Value
ws.Cells(irow, 4).Value = Me.ComboBox1.Value
ws.Cells(irow, 5).Value = Me.TextBox5.Value
ws.Cells(irow, 6).Value = Me.TextBox6.Value
ws.Cells(irow, 7).Value = Me.TextBox11.Value
'ws.Cells(irow, 8).Value = Me.TextBox8.Value
'ws.Cells(irow, 9).Value = Me.TextBox9.Value
'ws.Cells(irow, 10).Value = Me.TextBox11.Value
'ws.Cells(irow + 1, 1).Value = Me.TextBox1.Value
'ws.Cells(irow + 1, 2).Value = Me.TextBox2.Value
'ws.Cells(irow + 1, 3).Value = Me.TextBox4.Value
'ws.Cells(irow + 1, 4).Value = Me.ComboBox1.Value
'ws.Cells(irow + 1, 5).Value = Me.TextBox5.Value
'ws.Cells(irow + 1, 6).Value = Me.TextBox6.Value
'ws.Cells(irow + 1, 7).Value = Me.TextBox7.Value
'ws.Cells(irow + 1, 8).Value = Me.TextBox8.Value
'ws.Cells(irow + 1, 9).Value = Me.TextBox9.Value
'ws.Cells(irow + 1, 10).Value = Me.TextBox10.Value
'Clear the data
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox11.Value = ""
'Me.TextBox8.Value = ""
'Me.TextBox9.Value = ""
'Me.TextBox10.Value = ""
'Me.TextBox11.Value = ""
'Me.TextBox1.SetFocus
If CommandButton1 Then
UserForm1.Hide
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub Label7_Click()
End Sub
Private Sub TextBox2_Change()
TextBox2.Value = Format(Date, "DD/MM/YYYY") & Format(Time(), "HH:MM:SS")
End Sub
Private Sub TextBox7_Change()
End Sub
Private Sub UserForm_Initialize()
Me.ComboBox1.RowSource = ("Other!A2:E50")
End Sub
TextBox12-12 คืออันไหน ต้องการนำค่าจากที่ใดมาโชว์ เขียนไว้แล้วที่บรรทัดไหนครับlingnoi44 wrote: Fri Dec 21, 2018 2:54 pm รบกวนสอบถามเพิ่มเติมนะค่ะ ถ้าจะทำให้โชว์ค่าใน TextBox เพิ่มเติม ต้องเขียน Code ยังไงค่ะ มันขึ้น Error ค่ะ ;( ต้องการให้โชว์ใน TextBox12-12 ค่ะ
Code: Select all
Private Sub ComboBox1_Change()
TextBox7.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:E2"), 2, False)
TextBox8.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:E2"), 3, False)
TextBox9.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:E2"), 4, False)
TextBox10.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:E2"), 5, False)
TextBox12.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 2, False)
TextBox13.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 3, False)
TextBox14.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 4, False)
TextBox15.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 5, False)
End Sub
.Range("A2:E2")
และ .Range("A3:E3")
ให้ปรับเป็นช่วงข้อมูลทั้งหมดที่จะมีในชีต Other เช่น A2:A10000
เป็นต้นCode: Select all
Private Sub ComboBox1_Change()
'Select Case ComboBox1.ListIndex
'Case 0
'a = "Other!A2:A100"
'ComboBox1.RowSource = a
'End Select
TextBox7.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:A1000"), 2, False)
TextBox8.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:A1000"), 3, False)
TextBox9.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:A1000"), 4, False)
TextBox10.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A2:A1000"), 5, False)
'TextBox12.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 2, False)
'TextBox13.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 3, False)
'TextBox14.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 4, False)
'TextBox15.Value = Application.VLookup(Me.ComboBox1, Sheets("Other").Range("A3:E3"), 5, False)
End Sub
A2:E1000
เช่นนี้เป็นต้นครับ