:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser
🪷 คำแสดงเจตนา
ขอผลแห่งการให้ความรู้นี้ จงกลับไปยังผู้ที่เป็นเจ้าของเดิม แม้ข้าพเจ้าจะไม่รู้จักท่านก็ตาม ขอให้แสงแห่งปัญญาที่ท่านเคยจุดไว้ ได้กลับไปเติมเต็มชีวิตของท่านอีกครั้ง และขอให้เจตนาของข้าพเจ้าเป็นการคืนความดีอย่างสงบ

การเรียกฟอร์ม

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

การเรียกฟอร์ม

#1

Post by yodpao.b »

อยากสอบถามอาจาร์ยครับ
การคลิกในช่องเซลของ Excel นั้นสามารถส่งให้เรียกฟอร์มได้ไหมครับโดยไม่ต้องสร้างปุ่ม
ถ้าได้จะเขียนอย่างไรครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#2

Post by snasui »

:D ได้ครับ สามารถใช้ 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
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#3

Post by yodpao.b »

ขอบคุณครับ
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#4

Post 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
แก้ไขอย่างไรครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#5

Post by snasui »

:D การตรวจสอบว่าเซลล์ที่เลือกอยู่ในช่วงเซลล์ใด ๆ หรือไม่ สามารถใช้ 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
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#6

Post by yodpao.b »

เหมือเดิมครับไม่เปลียนแปลง
แนบไฟล์ผรยมาให้อาจารย์ดู
ขอโทษลืมไป
หน้าที่ทำงานหรือใช้ลิงค์ไปคือ sheet FormPrint
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#7

Post by snasui »

:D ลอง 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
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#8

Post by yodpao.b »

ได้แล้วครับอาจาร์ย
แต่ขอถามว่าที่ดูตัวอย่างแรกกับตัวอย่างที่2นั้นเหมือนกัน
แล้วอยู่ทำไม่ตัวอย่างที่2ถึงใช้ได้
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#9

Post by snasui »

:D การลำดับ Statement ก่อนหลังมีผลกับการแสดงผลครับ ปกติก่อนที่จะ Show เราควร Assign ค่าเสียก่อน และหากเกิดการกระทำใด ๆ (Event) จึงค่อยมาเปลี่ยนค่าใน Control (TextBox, ListBox, ComboBox, Label ฯลฯ) ต่าง ๆ อีกที

สังเกตได้ว่า Code แรกเป็นการ Show Form ขึ้นมาก่อนแล้วค่อยให้ TextBox1 เป็นค่าใด ๆ แต่ Code ที่ 2 Assign ค่าให้กับ TextBox1 เสียก่อนแล้วค่อย Show
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#10

Post by yodpao.b »

พอจะเข้าใจแล้วครับขอบคุณครับ
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#11

Post 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
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#12

Post by snasui »

:D ไม่เข้าใจครับ

ช่วยอธิบายขั้นตอนการทำงาน ช่วยยกตัวอย่างการกรอกข้อมูลและแจ้งปัญหาที่พบ ระบุผลลัพธ์ที่ต้องการหากโปรแกรมทำงานถูกต้องมาด้วยครับ
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#13

Post by yodpao.b »

ขอโทษครับตอนเมื่อกี้หลับในล้างหน้าแล้วครับ

หลังจากที่ฟอร์มโชว์และทุกช่องมีข้อมูลแล้ว ok ครับ
ขั้นต่อไปถ้าเราอยากจะแก้ไขข้อมูลในฟอรมโดยใช้boxแบบเลือก
ผมได้เขียน code แล้ว มันก็ใช้ได้ ครับ
แต่พอนำมารวมกันกลับพบว่าฟอร์มที่เรียกโชว์กลับไม่เห็นข้อมูล
เนื่องจาก textbox01 และ 02 ทำงานทับกันอยู่
โคด้ด้านล่างนี้จะอยู่ในฟอร์ม Form_5Edit
ผมได้แนบไฟล์แก้ไขทั้งหมดให้อาจาร์ยดูครับผม
ตอนนี้ต้องกลับบ้านก่อนครับเดี๋ยวถึงบ้านจะเปิดดูคำตอบของอาจาร์ย
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#14

Post by snasui »

:D ช่วยให้รายละเอียดตามด้านล่างด้วยครับ
snasui wrote::D ไม่เข้าใจครับ

ช่วยอธิบายขั้นตอนการทำงาน ช่วยยกตัวอย่างการกรอกข้อมูลและแจ้งปัญหาที่พบ ระบุผลลัพธ์ที่ต้องการหากโปรแกรมทำงานถูกต้องมาด้วยครับ
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#15

Post by yodpao.b »

ผมได้ทำข้อมูลออกมา 2 ไฟล์
ไฟล์book1.xls คือไฟล์ใช้งาน
ไฟล์ขั้นตอน.xls คือขั้นตอนการทำงาน แบ่งเป็น 3 หน้า
หน้าแรกคือ ขั้นตอนการทำงานและตัวอย่างที่ถูกต้อง
หน้าที่ 2 คือ ลักษณะการทำงานและผลลัพทของโปรแกรมที่ถูกต้อง
หน้าที่ 3 คือ ลักษณะการทำงานที่ผิด
ถ้าอาจาร์ยสังสัยตรงไหนก็ถามมาได้เลยนะครับเพราะผมไม่แน่ใจว่าผมซื่อความหมายถูกต้องไหม
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#16

Post by snasui »

:lol: ปกติการตั้งชื่อ 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
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#17

Post by yodpao.b »

ใช้ได้แล้วครับแปลงนิดหน่อย
คำถามต่อไป
เขียนอย่างไรให้ใน textbox เวลาคีย์ให้แสดงจุดทศนิยมด้วย
Untitled.gif
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#18

Post by snasui »

:D ลองตามตัวอย่างด้านล่างครับ

Code: Select all

TextBox1.Text = Format(TextBox1.Text, "0.00")
yodpao.b
Gold
Gold
Posts: 1608
Joined: Tue Jul 19, 2011 2:47 pm
Excel Ver: 2013,excel standard

Re: การเรียกฟอร์ม

#19

Post 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 รูปแบบทศนิยมถึงเปลี่ยนได้ไหมครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31176
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: การเรียกฟอร์ม

#20

Post by snasui »

:lol:
yodpao.b wrote:คำถามคือ จาก การคีย์ตัวเลขถึงจะเปลี่ยนรูปแบบทศนิยม เปลียนเป็นคลิก TextBox25 รูปแบบทศนิยมถึงเปลี่ยนได้ไหมครับ
ควรจะเป็นเช่นนั้น ไม่ใช่ให้เปลี่ยนเมื่อคีย์ ถ้าคีย์ไปเปลี่ยนไปรูปแบบที่ไม่พึงประสงค์ย่อมเกิดขึ้นได้ คีย์ให้จบก่อนแล้วค่อยเปลี่ยน หรือเกิด Event อื่นใดก่อน เช่นคลิก Control อื่น หรือ Show Form ฯลฯ แล้วค่อยเปลี่ยนครับ
Post Reply