Page 1 of 1
กด Enter แล้วคลุมข้อความ
Posted: Wed Jan 25, 2017 7:15 pm
by pong2532
สวัสดีครับอาจารย์ทุกท่าน
ผมมีปัญหาเกี่ยวกับการใช้ Code ครับ อยากจะขอคำแนะนำดังนี้ครับ
ผมสร้าง Userform มาใช้งานเวลากด Enter แล้วอยากให้มันคลุมข้อความตามรูปครับ
Capture.JPG
การทำงานคือ พิมพ์ตัวเลขใน Textbox แล้วกดปุ่ม Test บน Form
แต่ต้องการอยากให้ กด Enter ที่ คีย์บอร์ดแทนครับ
และเมื่อ Enter แล้ว Textbox พร้อมที่จะรับข้อมูลเข้าไปไหม่ คือคุมข้อความเก่าตามรูปครับ
Code: Select all
Private Sub CommandButton1_Enter()
Call CommandButton1_Click
TextBox2.SetFocus
รบกวนชี้แนะด้วยครับ
Re: กด Enter แล้วคลุมข้อความ
Posted: Wed Jan 25, 2017 8:46 pm
by snasui

ลบ Control อื่นให้หมดเหลือไว้เฉพา Label และ TextBox2 จากนั้นปรับ Code ตามตัวอย่างด้านล่างครับ
Code: Select all
Private Sub ChangeLabel()
If Label1 = "เปลี่ยนชื่อแล้ว" Then
GoTo pong
End If
Label1 = "เปลี่ยนชื่อแล้ว"
Label1.BackColor = &H80FF80
pong:
End Sub
Private Sub CheckChange()
Dim LResult As String
Label1 = ""
Label1.BackColor = &H8000000F
On Error GoTo pong
Range("G6").Select
Cells.Range("G6:G600").Find(What:=TextBox2, _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
TextBox3.Value = ActiveCell.Value
ActiveCell.Offset(0, -4).Select
LResult = Left(ActiveCell.Value, 7)
TextBox1.Value = LResult
Label1 = TextBox4 + "-" + TextBox1
pong:
End Sub
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
Call CheckChange
With Me.TextBox2
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub
Re: กด Enter แล้วคลุมข้อความ
Posted: Thu Jan 26, 2017 11:08 am
by pong2532
snasui wrote:
ลบ Control อื่นให้หมดเหลือไว้เฉพา Label และ TextBox2 จากนั้นปรับ Code ตามตัวอย่างด้านล่างครับ
Code: Select all
Private Sub ChangeLabel()
If Label1 = "เปลี่ยนชื่อแล้ว" Then
GoTo pong
End If
Label1 = "เปลี่ยนชื่อแล้ว"
Label1.BackColor = &H80FF80
pong:
End Sub
Private Sub CheckChange()
Dim LResult As String
Label1 = ""
Label1.BackColor = &H8000000F
On Error GoTo pong
Range("G6").Select
Cells.Range("G6:G600").Find(What:=TextBox2, _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
TextBox3.Value = ActiveCell.Value
ActiveCell.Offset(0, -4).Select
LResult = Left(ActiveCell.Value, 7)
TextBox1.Value = LResult
Label1 = TextBox4 + "-" + TextBox1
pong:
End Sub
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
Call CheckChange
With Me.TextBox2
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub
ได้แล้วครับ ขอบคุณมากครับอาจารย์
แต่ติดปัญหาถ้า เพิ่ม Listbox หรือ Textbox บน Userform จะใช้ไม่ได้ครับ ต้องปรับยังไงบ้างครับ
Code: Select all
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
Me.Label1 = "กด Enter แล้ว "
With Me.TextBox2
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub
Re: กด Enter แล้วคลุมข้อความ
Posted: Thu Jan 26, 2017 7:12 pm
by snasui

Code ตามไฟล์แนบไม่เป็นไปตามที่ผมตอบไปด้านบน ช่วยแนบไฟล์ที่ได้นำ Code นั้นไปใช้แล้วยังติดปัญหาครับ