Page 1 of 2
การเรียกฟอร์ม
Posted: Wed Jan 25, 2012 2:16 pm
by yodpao.b
อยากสอบถามอาจาร์ยครับ
การคลิกในช่องเซลของ Excel นั้นสามารถส่งให้เรียกฟอร์มได้ไหมครับโดยไม่ต้องสร้างปุ่ม
ถ้าได้จะเขียนอย่างไรครับ
Re: การเรียกฟอร์ม
Posted: Wed Jan 25, 2012 2:21 pm
by snasui

ได้ครับ สามารถใช้ SelectionChange Event เข้ามาช่วยได้
ยกตัวอย่างหากคลิกที่ A1 ใน Sheet1 แล้วให้แสดง Userform1 สามารถเข้าไปเลือก Event ใน Sheet1 แล้วเขียน Code ตามด้านล่างครับ
Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then UserForm1.Show
End Sub
Re: การเรียกฟอร์ม
Posted: Wed Jan 25, 2012 2:43 pm
by yodpao.b
ขอบคุณครับ
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 12:14 pm
by yodpao.b
เมื่อคลิกในเซลนั้นฟอร์มโชว์และต้องการให้แสดงตัวเลขในฟอร์มเช่นแสดงที่ textbox1 ว่าเป็นเลข1
ทำแล้วตามแสดงโคดด้านล่าง
Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$I$13:$N$13" Then Form_4Edit.Show
If Target.Address = "$I$15:$K$15" Then Form_4Edit.Show
If Target.Address = "$I$17:$K$17" Then Form_4Edit.Show
If Target.Address = "$I$19:$K$19" Then Form_4Edit.Show
If Target.Address = "$V$13:$AB$13" Then Form_4Edit.Show
If Target.Address = "$V$15:$AB$17" Then Form_4Edit.Show
If Target.Address = "$V$19:$AB$19" Then Form_4Edit.Show
If Target.Address = "$V$21:$X$21" Then Form_4Edit.Show
If Target.Address = "$F$25:$G$25" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "1"
End If
If Target.Address = "$F$27:$G$27" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "2"
End If
If Target.Address = "$F$29:$G$29" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "3"
End If
If Target.Address = "$F$31:$G$31" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "4"
End If
If Target.Address = "$F$33:$G$33" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "5"
End If
If Target.Address = "$F$35:$G$35" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "6"
End If
If Target.Address = "$F$37:$G$37" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "7"
End If
If Target.Address = "$F$39:$G$39" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "8"
End If
If Target.Address = "$F$41:$G$41" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "9"
End If
If Target.Address = "$F$43:$G$43" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "10"
End If
If Target.Address = "$F$45:$G$45" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "11"
End If
If Target.Address = "$F$47:$G$47" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "12"
End If
If Target.Address = "$F$45:$G$45" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "13"
End If
If Target.Address = "$F$51:$G$51" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "14"
End If
End Sub
ผลปัญหาดังนี้ ยกตัวอย่างโคดนี้
If Target.Address = "$F$25:$G$25" Then
Form_5Edit.Show
Form_5Edit!txtBox1.Text = "1"
End If
เมื่อมันโชว์ฟอร์มแล้ว พบว่าtxtBox1ไม่โชว์เลข1
แต่เมื่อคลิกอีกเซลหนึ่ง ซึ่งเซลนี้txtBox1ต้องโชว์เลข2 กลับโชว1
และเมื่อคลิกอีกเซลหนึ่ง ซึ่งเซลนี้txtBox1ต้องโชว์เลข3 กลับโชว2
แก้ไขอย่างไรครับ
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 1:15 pm
by snasui

การตรวจสอบว่า
เซลล์ที่เลือกอยู่ในช่วงเซลล์ใด ๆ หรือไม่ สามารถใช้ Code ตามด้านล่าง
ลอง Run ดูครับ หากยังไม่ได้ช่วยแนบไฟล์ตัวอย่างมาด้วย จะได้ช่วยทดสอบได้ครับ
Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("I13:N13,I15:K15,I17:K17,I19:K19,V13:AB13,V19:AB19,V21:X21")) _
Is Nothing Then
Form_4Edit.Show
End If
If Not Intersect(Target, Range("$F$25:$G$25")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "1"
End If
If Not Intersect(Target, Range("$F$27:$G$27")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "2"
End If
If Not Intersect(Target, Range("$F$29:$G$29")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "3"
End If
If Not Intersect(Target, Range("$F$31:$G$31")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "4"
End If
If Not Intersect(Target, Range("$F$33:$G$33")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "5"
End If
If Not Intersect(Target, Range("$F$35:$G$35")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "6"
End If
If Not Intersect(Target, Range("$F$37:$G$37")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "7"
End If
If Not Intersect(Target, Range("$F$39:$G$39")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "8"
End If
If Not Intersect(Target, Range("$F$41:$G$41")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "9"
End If
If Not Intersect(Target, Range("$F$43:$G$43")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "10"
End If
If Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "11"
End If
If Not Intersect(Target, Range("$F$47:$G$47")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "12"
End If
If Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "13"
End If
If Not Intersect(Target, Range("$F$51:$G$51")) Is Nothing Then
Form_5Edit.Show
Form_5Edit.txtBox1.Text = "14"
End If
End Sub
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 1:57 pm
by yodpao.b
เหมือเดิมครับไม่เปลียนแปลง
แนบไฟล์ผรยมาให้อาจารย์ดู
ขอโทษลืมไป
หน้าที่ทำงานหรือใช้ลิงค์ไปคือ sheet FormPrint
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 2:16 pm
by snasui

ลอง Assign ค่าให้กับ TextBox ก่อนแล้วค่อย Show ตาม Code ด้านล่างครับ
Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("I13:N13,I15:K15,I17:K17,I19:K19,V13:AB13,V19:AB19,V21:X21")) _
Is Nothing Then
Form_4Edit.Show
End If
If Not Intersect(Target, Range("$F$25:$G$25")) Is Nothing Then
Form_5Edit.txtBox1.Text = "1"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$27:$G$27")) Is Nothing Then
Form_5Edit!txtBox1.Text = "2"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$29:$G$29")) Is Nothing Then
Form_5Edit!txtBox1.Text = "3"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$31:$G$31")) Is Nothing Then
Form_5Edit.txtBox1.Text = "4"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$33:$G$33")) Is Nothing Then
Form_5Edit.txtBox1.Text = "5"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$35:$G$35")) Is Nothing Then
Form_5Edit.txtBox1.Text = "6"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$37:$G$37")) Is Nothing Then
Form_5Edit.txtBox1.Text = "7"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$39:$G$39")) Is Nothing Then
Form_5Edit.txtBox1.Text = "8"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$41:$G$41")) Is Nothing Then
Form_5Edit.txtBox1.Text = "9"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$43:$G$43")) Is Nothing Then
Form_5Edit.txtBox1.Text = "10"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.txtBox1.Text = "11"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$47:$G$47")) Is Nothing Then
Form_5Edit.txtBox1.Text = "12"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.txtBox1.Text = "13"
Form_5Edit.Show
End If
If Not Intersect(Target, Range("$F$51:$G$51")) Is Nothing Then
Form_5Edit.txtBox1.Text = "14"
Form_5Edit.Show
End If
End Sub
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 2:33 pm
by yodpao.b
ได้แล้วครับอาจาร์ย
แต่ขอถามว่าที่ดูตัวอย่างแรกกับตัวอย่างที่2นั้นเหมือนกัน
แล้วอยู่ทำไม่ตัวอย่างที่2ถึงใช้ได้
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 2:38 pm
by snasui

การลำดับ Statement ก่อนหลังมีผลกับการแสดงผลครับ ปกติก่อนที่จะ Show เราควร Assign ค่าเสียก่อน และหากเกิดการกระทำใด ๆ (Event) จึงค่อยมาเปลี่ยนค่าใน Control (TextBox, ListBox, ComboBox, Label ฯลฯ) ต่าง ๆ อีกที
สังเกตได้ว่า Code แรกเป็นการ Show Form ขึ้นมาก่อนแล้วค่อยให้ TextBox1 เป็นค่าใด ๆ แต่ Code ที่ 2 Assign ค่าให้กับ TextBox1 เสียก่อนแล้วค่อย Show
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 2:46 pm
by yodpao.b
พอจะเข้าใจแล้วครับขอบคุณครับ
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 3:25 pm
by yodpao.b
อาจาร์ยครับคำถามใหม่คร้บ
หลังจากที่ฟอร์มโชว์และทุกช่องมีข้อมูลแล้ว ok ครับ
ขั้นต่อไปถ้าเราอยากจะแก้ไขข้อมูลในฟอรมโดยใช้boxแบบเลือก
ผมได้เขียน code แล้ว มันก็ใช้ได้ ครับ
แต่พอนำมารวมกันกลับพบว่าฟอร์มที่เรียกโชว์กลับไม่เห็นข้อมูล
เนื่องจาก textbox 1 และ 2 ทำงานทับกันอยู่
โคด้ด้านล่างนี้จะอยู่ในฟอร์ม
Form_5Edit
Code: Select all
Private Sub TextBox01_Change()
Sheets("data_T5").Select
'Range("C3:O3").ClearContents
'Range("AL9:AM200").ClearContents
'Range("E3") = TextBox01.Text
'Call MacroAdvancedFilter1
'Call MacroPasteValuesAL
'Call MacroShowAllFilter
TextBox02.Clear
ListBox1.Clear
Range("AL10").Select
Do While Not IsEmpty(ActiveCell.Value)
If MyTextBox02 <> ActiveCell.Value Then
MyTextBox02 = ActiveCell.Value
TextBox02.AddItem MyTextBox02
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Private Sub TextBox02_Change()
Sheets("data_T5").Select
'Range("C3:O3").ClearContents
'Range("AM9:AM200").ClearContents
'ListBox1.Clear
'TextBox21 = ""
'TextBox22 = ""
'TextBox23 = ""
'TextBox24 = ""
'TextBox25 = ""
'Range("E3") = TextBox01
'Range("H3") = TextBox02
'Call MacroAdvancedFilter1
'Call MacroPasteValuesAM
'Call MacroShowAllFilter
Range("AM10").Select
Do While Not IsEmpty(ActiveCell.Value)
ListBox1.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 3:38 pm
by snasui

ไม่เข้าใจครับ
ช่วยอธิบายขั้นตอนการทำงาน ช่วยยกตัวอย่างการกรอกข้อมูลและแจ้งปัญหาที่พบ ระบุผลลัพธ์ที่ต้องการหากโปรแกรมทำงานถูกต้องมาด้วยครับ
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 3:52 pm
by yodpao.b
ขอโทษครับตอนเมื่อกี้หลับในล้างหน้าแล้วครับ
หลังจากที่ฟอร์มโชว์และทุกช่องมีข้อมูลแล้ว ok ครับ
ขั้นต่อไปถ้าเราอยากจะแก้ไขข้อมูลในฟอรมโดยใช้boxแบบเลือก
ผมได้เขียน code แล้ว มันก็ใช้ได้ ครับ
แต่พอนำมารวมกันกลับพบว่าฟอร์มที่เรียกโชว์กลับไม่เห็นข้อมูล
เนื่องจาก textbox01 และ 02 ทำงานทับกันอยู่
โคด้ด้านล่างนี้จะอยู่ในฟอร์ม Form_5Edit
ผมได้แนบไฟล์แก้ไขทั้งหมดให้อาจาร์ยดูครับผม
ตอนนี้ต้องกลับบ้านก่อนครับเดี๋ยวถึงบ้านจะเปิดดูคำตอบของอาจาร์ย
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 7:31 pm
by snasui

ช่วยให้รายละเอียดตามด้านล่างด้วยครับ
snasui wrote:
ไม่เข้าใจครับ
ช่วยอธิบายขั้นตอนการทำงาน ช่วยยกตัวอย่างการกรอกข้อมูลและแจ้งปัญหาที่พบ ระบุผลลัพธ์ที่ต้องการหากโปรแกรมทำงานถูกต้องมาด้วยครับ
Re: การเรียกฟอร์ม
Posted: Mon Jan 30, 2012 9:32 pm
by yodpao.b
ผมได้ทำข้อมูลออกมา 2 ไฟล์
ไฟล์book1.xls คือไฟล์ใช้งาน
ไฟล์ขั้นตอน.xls คือขั้นตอนการทำงาน แบ่งเป็น 3 หน้า
หน้าแรกคือ ขั้นตอนการทำงานและตัวอย่างที่ถูกต้อง
หน้าที่ 2 คือ ลักษณะการทำงานและผลลัพทของโปรแกรมที่ถูกต้อง
หน้าที่ 3 คือ ลักษณะการทำงานที่ผิด
ถ้าอาจาร์ยสังสัยตรงไหนก็ถามมาได้เลยนะครับเพราะผมไม่แน่ใจว่าผมซื่อความหมายถูกต้องไหม
Re: การเรียกฟอร์ม
Posted: Tue Jan 31, 2012 1:46 pm
by snasui

ปกติการตั้งชื่อ Control กับแบบของ Control ควรจะสอดคล้องกันครับ ถ้าเป็น ComboBox ปกติจะขึ้นต้นชื่อด้วย cb ยกตัวอย่างเช่น cbName, cbSignal ฯลฯ
ลองปรับ Code ใหม่เพื่อให้กระชับและแสดงผลที่ถูกต้องตามด้านล่างครับ
ที่ชีท FormPrint ปรับ Event Worksheet_SelectionChange เป็นด้านล่าง
Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("I13:N13,I15:K15,I17:K17,I19:K19,V13:AB13,V15:AB17,V19:AB19,V21:X21")) _
Is Nothing Then
Form_4Edit.Show
End If
If Not Intersect(Target, Range("$F$25:$G$25")) Is Nothing Then
Form_5Edit.txtBox1.Text = "1"
ElseIf Not Intersect(Target, Range("$F$27:$G$27")) Is Nothing Then
Form_5Edit!txtBox1.Text = "2"
ElseIf Not Intersect(Target, Range("$F$29:$G$29")) Is Nothing Then
Form_5Edit!txtBox1.Text = "3"
ElseIf Not Intersect(Target, Range("$F$31:$G$31")) Is Nothing Then
Form_5Edit.txtBox1.Text = "4"
ElseIf Not Intersect(Target, Range("$F$33:$G$33")) Is Nothing Then
Form_5Edit.txtBox1.Text = "5"
ElseIf Not Intersect(Target, Range("$F$35:$G$35")) Is Nothing Then
Form_5Edit.txtBox1.Text = "6"
ElseIf Not Intersect(Target, Range("$F$37:$G$37")) Is Nothing Then
Form_5Edit.txtBox1.Text = "7"
ElseIf Not Intersect(Target, Range("$F$39:$G$39")) Is Nothing Then
Form_5Edit.txtBox1.Text = "8"
ElseIf Not Intersect(Target, Range("$F$41:$G$41")) Is Nothing Then
Form_5Edit.txtBox1.Text = "9"
ElseIf Not Intersect(Target, Range("$F$43:$G$43")) Is Nothing Then
Form_5Edit.txtBox1.Text = "10"
ElseIf Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.txtBox1.Text = "11"
ElseIf Not Intersect(Target, Range("$F$47:$G$47")) Is Nothing Then
Form_5Edit.txtBox1.Text = "12"
ElseIf Not Intersect(Target, Range("$F$45:$G$45")) Is Nothing Then
Form_5Edit.txtBox1.Text = "13"
ElseIf Not Intersect(Target, Range("$F$51:$G$51")) Is Nothing Then
Form_5Edit.txtBox1.Text = "14"
End If
Form_5Edit.Show
End Sub
ที่ Form_5Edit เปลี่ยนที่ txtBox1_Change เป็นด้านล่าง
Code: Select all
Private Sub txtBox1_Change()
On Error Resume Next
Sheets("CalPrint").Select
Range("I11").Select
Do While ActiveCell <> ""
If CStr(ActiveCell.Value) = txtBox1.Text Then
TextBox01.Text = ActiveCell.Offset(0, 1).Value
TextBox02.Text = ActiveCell.Offset(0, 2).Value
Sheets("CalPrint").Select
TextBox03.Text = ActiveCell.Offset(0, 3).Value
ListBox1.Text = ActiveCell.Offset(0, 3).Value
TextBox21.Text = ActiveCell.Offset(0, 4).Value
TextBox22.Text = ActiveCell.Offset(0, 5).Value
TextBox23.Text = ActiveCell.Offset(0, 6).Value
TextBox24.Text = ActiveCell.Offset(0, 7).Value
TextBox25.Text = ActiveCell.Offset(0, 8).Value
Exit Sub
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Re: การเรียกฟอร์ม
Posted: Wed Feb 01, 2012 3:50 pm
by yodpao.b
ใช้ได้แล้วครับแปลงนิดหน่อย
คำถามต่อไป
เขียนอย่างไรให้ใน textbox เวลาคีย์ให้แสดงจุดทศนิยมด้วย
Untitled.gif
Re: การเรียกฟอร์ม
Posted: Wed Feb 01, 2012 4:05 pm
by snasui

ลองตามตัวอย่างด้านล่างครับ
Code: Select all
TextBox1.Text = Format(TextBox1.Text, "0.00")
Re: การเรียกฟอร์ม
Posted: Wed Feb 01, 2012 11:35 pm
by yodpao.b
ใช้แล้วครับ
ผลที่ได้พอคีย์ตัวเลข 6 จะได้ 6.00 Ok
แต่ถ้าคีย์ุตัวเลข 61 จะได้ 6.01 ผิดครับ
code ที่ใช้คือ
Code: Select all
Private Sub TextBox24_Change()
TextBox24.Text = Format(TextBox24.Text, "0.00")
End Sub
แต่ถ้าใช้Codeด้านล่างก็ OK ผมเข้าใจถูกใช่ไหม่ครับ
Code: Select all
Private Sub TextBox25_Change()
TextBox24.Text = Format(TextBox24.Text, "0.00")
End Sub
คำถามคือ จาก การคีย์ตัวเลขถึงจะเปลี่ยนรูปแบบทศนิยม เปลียนเป็นคลิก TextBox25 รูปแบบทศนิยมถึงเปลี่ยนได้ไหมครับ
Re: การเรียกฟอร์ม
Posted: Thu Feb 02, 2012 7:32 am
by snasui
yodpao.b wrote:คำถามคือ จาก การคีย์ตัวเลขถึงจะเปลี่ยนรูปแบบทศนิยม เปลียนเป็นคลิก TextBox25 รูปแบบทศนิยมถึงเปลี่ยนได้ไหมครับ
ควรจะเป็นเช่นนั้น
ไม่ใช่ให้เปลี่ยนเมื่อคีย์ ถ้าคีย์ไปเปลี่ยนไปรูปแบบที่ไม่พึงประสงค์ย่อมเกิดขึ้นได้ คีย์ให้จบก่อนแล้วค่อยเปลี่ยน หรือเกิด Event อื่นใดก่อน เช่นคลิก Control อื่น หรือ Show Form ฯลฯ แล้วค่อยเปลี่ยนครับ