snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
อยากทำให้ข้อมูลที่เรา add ลงใน UserForm1 Page1 ไปขึ้น show ในListBox ของpage2 ของ UserForm1 เมื่อคลิกปุ่ม
Add New Value to Database
เพื่อให้เวลาคลิกเลือกดู ค่าเก่าที่เคย add ข้อมูลไว้ใน Select user value in database
ลองใส่คำสั่งที่ปุ่ม Add New Value to Database แล้วแต่ compile error ค่ะ
รบกวนช่วยดูว่าต้องแก้ไขตรงไหนค่ะ
ขอบคุณนะคะ
แนบไฟล์มาด้วยนะคะ
You do not have the required permissions to view the files attached to this post.
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")
'check for a name
If Trim(Me.TextBox8_Name.Value) = "" Then
Me.TextBox8_Name.SetFocus
MsgBox "Please enter a name of raw material or chemical"
Exit Sub
End If
'check for a CF
If Trim(Me.TxtBox3.Value) = "" Then
Me.TxtBox3.SetFocus
MsgBox "Please enter value"
Exit Sub
End If
If Not IsNumeric(TxtBox3) Then
MsgBox "Please enter numeric."
Me.TxtBox3.Text = "0.00"
Exit Sub
End If
Answer = MsgBox("Do you want save change?", 1 + 32, "Create record")
If Answer = 1 Then
'find first empty row in database
iRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
'copy the data to the database
ws.Cells(iRow, 2).Value = Me.TextBox2.Value
ws.Cells(iRow, 3).Value = Me.TextBox8_Name.Value
ws.Cells(iRow, 4).Value = Me.TxtBox3.Value
ws.Cells(iRow, 5).Value = "kg"
ws.Cells(iRow, 6).Value = Me.TxtBox4_Source.Value
ws.Cells(iRow, 7).Value = Me.TextBox5_Year.Value
ws.Cells(iRow, 8).Value = Me.TextBox6_Location.Value
ws.Cells(iRow, 9).Value = Me.TextBox7_Comment.Value
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")
'check for a name
If Trim(Me.TextBox8_Name.Value) = "" Then
Me.TextBox8_Name.SetFocus
MsgBox "Please enter a name of raw material or chemical"
Exit Sub
End If
'check for a CF
If Trim(Me.TxtBox3.Value) = "" Then
Me.TxtBox3.SetFocus
MsgBox "Please enter value"
Exit Sub
End If
If Not IsNumeric(TxtBox3) Then
MsgBox "Please enter numeric."
Me.TxtBox3.Text = "0.00"
Exit Sub
End If
Answer = MsgBox("Do you want save change?", 1 + 32, "Create record")
If Answer = 1 Then
'find first empty row in database
iRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
'copy the data to the database
ws.Cells(iRow, 2).Value = Me.TextBox2.Value
ws.Cells(iRow, 3).Value = Me.TextBox8_Name.Value
ws.Cells(iRow, 4).Value = Me.TxtBox3.Value
ws.Cells(iRow, 5).Value = "kg"
ws.Cells(iRow, 6).Value = Me.TxtBox4_Source.Value
ws.Cells(iRow, 7).Value = Me.TextBox5_Year.Value
ws.Cells(iRow, 8).Value = Me.TextBox6_Location.Value
ws.Cells(iRow, 9).Value = Me.TextBox7_Comment.Value
'clear the data
Me.TextBox8_Name.Value = ""
Me.TxtBox3.Value = ""
Me.TxtBox4_Source.Value = ""
Me.TextBox5_Year.Value = ""
Me.TextBox6_Location.Value = ""
Me.TextBox7_Comment.Value = ""
'Unload Me
'ElseIf Answer = 2 Then
' Unload Me
'Exit Sub
End If
'If Page1.TextBox8_Name.Value <> "" Then
'With ThisWorkbook.Worksheets("Database")
'Row = .Cells(2, 3).End(xlDown).Row + 1
'.Cells(Row, 3).Value = UserForm1.TextBox8_Name.Value
'End With
'Call UserForm1.ListBox1
'UserForm2.TextBox1.Value = ""
'End If
'TextBox2.Text = TextBox1.Text
ListBox1.AddItem = TextBox8_Name
End Sub
'...
dim rall as range
dim r as range
with sheets("exam")
set rall = .range("a1", .range("a" & rows.count).end(xlup))
end with
for each r in rall
lisbox1.additem r
next r
'...
Private Sub TextBox8_Name_Change()
Dim rall As Range
Dim r As Range
With Sheets("database")
Set rall = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
End With
For Each r In rall
Lisbox1.AddItem r
Next r
UserForm1.ListBox1.AddItem (UserForm1.TextBox8_Name.Text)
End Sub
Private Sub TextBox8_Name_Change()
Dim rall As Range
Dim r As Range
With Sheets("database")
Set rall = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
End With
For Each r In rall
ListBox1.AddItem r ' Change lisbox1 to ListBox1
Next r
'UserForm1.ListBox1.AddItem (UserForm1.TextBox8_Name.Text)
End Sub
Private Sub CommandButton7_Click()
Dim lng As Long
Answer = MsgBox("Are you sure you want to delete data from database?", 4 + 48, "Delete database")
If Answer = 6 Then
lng = Application.Match(ListBox1.Value, Worksheets("Database").Range("C:I"), 0)
Worksheets("Database").Rows(lng).Delete
Unload Me
ElseIf Answer = 7 Then
Unload Me
Exit Sub
End If
End Sub
ขอบคุณมากค่ะ
You do not have the required permissions to view the files attached to this post.
สิ่งที่พบคือช่อง Name เป็น Number บ้างเป็น Text บ้าง ซึ่งไม่ทราบว่าข้อมูลจริงมีลักษณะเป็นอย่างไร หากข้อมูลจริงมี Text บ้าง Number เช่นนี้แล้ว เพื่อให้ง่ายผมแนะนำให้เปลี่ยนข้อมูลต้นแหล่งให้เป็น Text ท้้งหมด เนื่องจากเรากำลังจะนำค่าใน TextBox ไป Lookup และค่าใน TextBox จะเป็น Text เสมอ
Text จะเป็น ตัวเลขล้วนก็ได้แต่ถูกจัด Format ให้เป็น Text ส่วนตัวเลขผสมตัวอักษรและตัวอักษรล้วนจะถูกจัดให้เป็น Text อยู่แล้ว
Number จำเป็นที่จะต้องเป็นตัวเลขอย่างเดียวและจัด Format ให้เป็น Number ถ้าจัด Format ให้เป็น Text ก็จะกลายเป็น Text
จากกรณีนี้ผมเข้าใจว่า Name จะมีการคีย์ตัวเลขบ้าง ตัวอักษรบ้าง ปนกันบ้าง แต่ควรจะมี Format เป็น Text ไม่ใช่มี Format ผสมกัน และหากเป็นเช่นนั้น ควรจัด Format ในช่อง Name ให้เป็น Text เพราะจะได้ง่ายต่อการ Lookup จาก TextBox ที่มีสถานะเป็น Text เนื่องจากการ Lookup จะดูการจัด Format เป็นสำคัญ เราไม่สามารถ Lookup ข้าม Format กันได้
ตัวอย่างการจัด Format ให้เป็น Text ด้วย Manual
คลุมช่อง Name > คลิกขวา > Format Cells... > ที่แถบ Number เลือกเป็น Text
แต่หากกรณีต้องการจะใช้ Format ในช่อง Name ผสมกันไปเช่นนั้นจำเป็นจะต้องเขียน Code ในการจัดการค่าใน TextBox หรือในช่อง Name อย่างใดอย่างหนึ่งเพื่อให้ Lookup กันได้ ส่วนจะเขียนอย่างไรนั้นให้ลองเขียนก่อนครับ ติดตรงไหนก็มาดูกันต่อไป
Private Sub ListBox1_SelectNewRW_Click()
With Worksheets("UsEF_RW")
TextBox14_SelectCF.Value = Application.VLookup(Me.ListBox1_SelectNewRW, .Range("E15:Q100"), 10, False)
TextBox12_SelectSource.Value = Application.VLookup(Me.ListBox1_SelectNewRW, .Range("E15:Q100"), 12, False)
TextBox11_SelectYear.Value = Application.VLookup(Me.ListBox1_SelectNewRW, .Range("E15:Q100"), 13, False)
TextBox10_SelectLocation.Value = Application.VLookup(Me.ListBox1_SelectNewRW, .Range("E15:Q100"), 14, False)
TextBox9_SelectComment.Value = Application.VLookup(Me.ListBox1_SelectNewRW, .Range("E15:Q100"), 15, False)
End With
End Sub
Private Sub TextBox14_SelectCF_Change()
TextBox14_SelectCF.Value = Application.VLookup(Me.ListBox1_SelectNewRW, Worksheets("UsEF_RW").Range("E15:Q100"), 6, False)
End Sub
Private Sub TextBox12_SelectSource_Change()
TextBox12_SelectSource.Value = Application.VLookup(Me.ListBox1_SelectNewRW, Worksheets("UsEF_RW").Range("E15:Q100"), 8, False)
End Sub
Private Sub TextBox11_SelectYear_Change()
TextBox11_SelectYear.Value = Application.VLookup(Me.ListBox1_SelectNewRW, Worksheets("UsEF_RW").Range("E15:Q100"), 9, False)
End Sub
Private Sub TextBox10_SelectLocation_Change()
TextBox10_SelectLocation.Value = Application.VLookup(Me.ListBox1_SelectNewRW, Worksheets("UsEF_RW").Range("E15:Q100"), 10, False)
End Sub
Private Sub TextBox9_SelectComment_Change()
TextBox9_SelectComment.Value = Application.VLookup(Me.ListBox1_SelectNewRW, Worksheets("UsEF_RW").Range("E15:Q100"), 11, False)
End Sub
You do not have the required permissions to view the files attached to this post.