Page 1 of 2

เรื่องวันที่ใน Userform

Posted: Wed Oct 15, 2014 9:14 pm
by boyanny28
วันที่ในUserform.jpg
ผมดึงข้อมูลจาก database มาแสดงใน userform1 มาเพื่อการแก้ไข
แต่ทำไมวันที่ ที่แสดงใน Textbox ไม่แสดงวันที่ครับ แต่แสดงเป็น4xxxxครับ
ผมต้องแก้ตรงไหนครับ

Re: เรื่องวันที่ใน Userform

Posted: Thu Oct 16, 2014 4:18 pm
by snasui
:D แจ้ง Code ที่ใช้ดึงวันที่มาด้วยครับ

Re: เรื่องวันที่ใน Userform

Posted: Thu Oct 16, 2014 8:02 pm
by boyanny28
ผมใช้ Vlookup ธรรมดา รบกวนดูตาม File แนบครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 7:13 am
by snasui
:D ใน TextBox ข้อมูลจะกลายเป็น Text หรือ String หากต้องการให้เป็น Format วันที่ ต้องทำการกำหนดเองครับ เช่น

Code: Select all

TextBox1.Value = Format(Application.WorksheetFunction.VLookup(CLng(ComboBox1), myRange, 2, False), "dd/mm/yyyy")

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 8:07 am
by boyanny28
ขอบคุณพี่พงษ์ครับ เดี๋ยวผมขอลองก่อนครับ แล้วจะกลับมารายงานผลครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 8:10 am
by boyanny28
ได้ตามที่ต้องการแล้วครับ ขอบคุณครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 8:39 am
by boyanny28
ขอถามต่อนะครับ Format ถ้าเราต้องการเป็น ค.ศ.ต้องใช้ Format แบบไหนครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 8:43 am
by snasui
:D สามารถปรับ Code เป็นด้านล่างครับ

Code: Select all

TextBox1.Text = Application.Text(Application.WorksheetFunction.VLookup(CLng(ComboBox1), myRange, 2, False), "[$- ]dd/mm/yyyy")

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 9:20 am
by boyanny28
ขอบคุณครับ แต่พอปรับโค๊ดแล้ว ใน Database ที่เป็นค่าว่างๆ แต่ใน Textbox แสดง 00/01/1900 ครับ ผมต้องศึกษาคำสั่งไหนเพิ่มเติมครับ
ถ้าผมต้องการให้ค่าว่างใน Database มาแสดงใน Textbox ก็เป็นค่าว่างเหมือนกันครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 9:33 am
by snasui
:D สามารถดักด้วย If อีกรอบหลังบรรทัดเดิม เป็น

Code: Select all

if vba.left(textbox1.text,2) = "00" then textbox1.text = ""

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 10:29 am
by boyanny28
พี่ครับ
IF VBA.Left นี่คือ โค๊ดที่อยู่ด้านซ้ายใช่ไหมครับ หรือให้ผมนำโค๊ดไปวางต่อท้ายเลยครับ
ผมลองวางต่อท้าย มันไม่ได้ครับ
ถ้าจะให้เข้า IF......Then .....End if
ผมลองทำดูแล้ว พอมีข้อมูลเพิ่มเข้ามาใน cell ที่อยู่ใน database มันก็ไม่แสดงข้อมูลครับ
รบกวนด้วยครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 10:38 am
by boyanny28
ได้แล้วครับ ผมทำผิดเองครับ :D

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 1:57 pm
by boyanny28
ใน Userform 1 ที่ผมทำขึ้นมาใหม่ เพื่อรับข้อมูลเข้า database
Textbox ที่ user กรอก อยู่ใน Format dd/mm/yyyy แต่พอ paste ใน sheet database กลับเป็น mm/dd/yyyy
เรามีวิธีทำให้อยู่ใน รูปแบบ dd/mm/yyyy ก่อนวางใน database ไหมครับ Private Sub CommandButton1_Click()
Dim r As Long
With Sheets("Database")
r = .Range("a" & Rows.Count).End(xlUp).Row + 1
.Cells(r, 1) = ComboBox1.Text
.Cells(r, 2) = TextBox1.Text
.Cells(r, 3) = TextBox2.Text
.Cells(r, 4) = TextBox3.Text
.Cells(r, 5) = TextBox4.Text
.Cells(r, 6) = TextBox5.Text
.Cells(r, 7) = TextBox6.Text
.Cells(r, 8) = TextBox7.Text
.Cells(r, 9) = TextBox8.Text
.Cells(r, 10) = TextBox9.Text
.Cells(r, 11) = TextBox10.Text
.Cells(r, 12) = TextBox11.Text
.Cells(r, 13) = TextBox12.Text
.Cells(r, 14) = TextBox13.Text
.Cells(r, 15) = TextBox14.Text
.Cells(r, 16) = TextBox15.Text
.Cells(r, 17) = TextBox16.Text
.Cells(r, 18) = TextBox17.Text
.Cells(r, 19) = TextBox18.Text
.Cells(r, 20) = TextBox19.Text
.Cells(r, 21) = TextBox20.Text
.Cells(r, 22) = TextBox21.Text
.Cells(r, 23) = TextBox22.Text
.Cells(r, 24) = TextBox23.Text
.Cells(r, 25) = TextBox24.Text
.Cells(r, 26) = TextBox25.Text
.Cells(r, 27) = TextBox26.Text
.Cells(r, 28) = TextBox27.Text
.Cells(r, 29) = TextBox28.Text
.Cells(r, 30) = TextBox29.Text
.Cells(r, 31) = TextBox30.Text
.Cells(r, 32) = TextBox31.Text
.Cells(r, 33) = TextBox32.Text
.Cells(r, 34) = TextBox33.Text
.Cells(r, 35) = TextBox34.Text
.Cells(r, 36) = TextBox35.Text
.Cells(r, 37) = TextBox36.Text
.Cells(r, 38) = TextBox37.Text
.Cells(r, 39) = TextBox38.Text
.Cells(r, 40) = TextBox39.Text
.Cells(r, 41) = TextBox40.Text
.Cells(r, 42) = TextBox41.Text
.Cells(r, 43) = TextBox42.Text
.Cells(r, 44) = TextBox43.Text
.Cells(r, 45) = TextBox44.Text
.Cells(r, 46) = TextBox45.Text
.Cells(r, 47) = TextBox46.Text
.Cells(r, 48) = TextBox47.Text
.Cells(r, 49) = TextBox48.Text
.Cells(r, 50) = TextBox49.Text
.Cells(r, 51) = TextBox50.Text
.Cells(r, 52) = TextBox51.Text
.Cells(r, 53) = TextBox52.Text
.Cells(r, 54) = TextBox53.Text
.Cells(r, 55) = TextBox54.Text
.Cells(r, 56) = TextBox55.Text
.Cells(r, 57) = TextBox56.Text
.Cells(r, 58) = TextBox57.Text
.Cells(r, 59) = TextBox58.Text
.Cells(r, 60) = TextBox59.Text
.Cells(r, 61) = TextBox60.Text
.Cells(r, 62) = TextBox61.Text
.Cells(r, 63) = TextBox62.Text
.Cells(r, 64) = TextBox63.Text
.Cells(r, 65) = TextBox64.Text
.Cells(r, 66) = TextBox65.Text
.Cells(r, 67) = TextBox66.Text
.Cells(r, 68) = TextBox67.Text
.Cells(r, 69) = TextBox68.Text
.Cells(r, 70) = TextBox69.Text
.Cells(r, 71) = TextBox70.Text
.Cells(r, 72) = TextBox71.Text
.Cells(r, 73) = TextBox72.Text
.Cells(r, 74) = TextBox73.Text
.Cells(r, 75) = TextBox74.Text
.Cells(r, 76) = TextBox75.Text
.Cells(r, 77) = TextBox76.Text
.Cells(r, 78) = TextBox77.Text
.Cells(r, 79) = TextBox78.Text
.Cells(r, 80) = TextBox79.Text
End With
End Sub

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 3:17 pm
by snasui
:D คุณ boyanny28 วาง Code ไม่ถูกต้อง ลองพยายามใหม่ตาม Link นี้ครับ viewtopic.php?style=1&f=3&t=1187

ลองดูว่าในเครื่องที่ใช้งานมีการกำหนด Regional and Language ไว้แบบใดแล้วช่วยแจ้งมาด้วยครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 3:32 pm
by boyanny28
Region ตามนี้ครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 3:34 pm
by snasui
:!: ผมลบความเห็นที่โพสต์มาเป็น Code ล่าสุดทิ้งไปเองเนื่องจากคุณโพสต์มาไม่ถูกต้อง ดูตาม Link ที่ผมให้ไปว่าทำอย่างไร คิดว่าผมทำไว้ชัดเจนดีแล้วครับ

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 3:35 pm
by boyanny28
ขอโทษครับ ผมวางใหม่นะครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim r As Long
With Sheets("Database")
r = .Range("a" & Rows.Count).End(xlUp).Row + 1
.Cells(r, 1) = ComboBox1.Text
.Cells(r, 2) = TextBox1.Text
.Cells(r, 3) = TextBox2.Text
.Cells(r, 4) = TextBox3.Text
.Cells(r, 5) = TextBox4.Text
.Cells(r, 6) = TextBox5.Text
.Cells(r, 7) = TextBox6.Text
.Cells(r, 8) = TextBox7.Text
.Cells(r, 9) = TextBox8.Text
.Cells(r, 10) = TextBox9.Text
.Cells(r, 11) = TextBox10.Text
.Cells(r, 12) = TextBox11.Text
.Cells(r, 13) = TextBox12.Text
.Cells(r, 14) = TextBox13.Text
.Cells(r, 15) = TextBox14.Text
.Cells(r, 16) = TextBox15.Text
.Cells(r, 17) = TextBox16.Text
.Cells(r, 18) = TextBox17.Text
.Cells(r, 19) = TextBox18.Text
.Cells(r, 20) = TextBox19.Text
.Cells(r, 21) = TextBox20.Text
.Cells(r, 22) = TextBox21.Text
.Cells(r, 23) = TextBox22.Text
.Cells(r, 24) = TextBox23.Text
.Cells(r, 25) = TextBox24.Text
.Cells(r, 26) = TextBox25.Text
.Cells(r, 27) = TextBox26.Text
.Cells(r, 28) = TextBox27.Text
.Cells(r, 29) = TextBox28.Text
.Cells(r, 30) = TextBox29.Text
.Cells(r, 31) = TextBox30.Text
.Cells(r, 32) = TextBox31.Text
.Cells(r, 33) = TextBox32.Text
.Cells(r, 34) = TextBox33.Text
.Cells(r, 35) = TextBox34.Text
.Cells(r, 36) = TextBox35.Text
.Cells(r, 37) = TextBox36.Text
.Cells(r, 38) = TextBox37.Text
.Cells(r, 39) = TextBox38.Text
.Cells(r, 40) = TextBox39.Text
.Cells(r, 41) = TextBox40.Text
.Cells(r, 42) = TextBox41.Text
.Cells(r, 43) = TextBox42.Text
.Cells(r, 44) = TextBox43.Text
.Cells(r, 45) = TextBox44.Text
.Cells(r, 46) = TextBox45.Text
.Cells(r, 47) = TextBox46.Text
.Cells(r, 48) = TextBox47.Text
.Cells(r, 49) = TextBox48.Text
.Cells(r, 50) = TextBox49.Text
.Cells(r, 51) = TextBox50.Text
.Cells(r, 52) = TextBox51.Text
.Cells(r, 53) = TextBox52.Text
.Cells(r, 54) = TextBox53.Text
.Cells(r, 55) = TextBox54.Text
.Cells(r, 56) = TextBox55.Text
.Cells(r, 57) = TextBox56.Text
.Cells(r, 58) = TextBox57.Text
.Cells(r, 59) = TextBox58.Text
.Cells(r, 60) = TextBox59.Text
.Cells(r, 61) = TextBox60.Text
.Cells(r, 62) = TextBox61.Text
.Cells(r, 63) = TextBox62.Text
.Cells(r, 64) = TextBox63.Text
.Cells(r, 65) = TextBox64.Text
.Cells(r, 66) = TextBox65.Text
.Cells(r, 67) = TextBox66.Text
.Cells(r, 68) = TextBox67.Text
.Cells(r, 69) = TextBox68.Text
.Cells(r, 70) = TextBox69.Text
.Cells(r, 71) = TextBox70.Text
.Cells(r, 72) = TextBox71.Text
.Cells(r, 73) = TextBox72.Text
.Cells(r, 74) = TextBox73.Text
.Cells(r, 75) = TextBox74.Text
.Cells(r, 76) = TextBox75.Text
.Cells(r, 77) = TextBox76.Text
.Cells(r, 78) = TextBox77.Text
.Cells(r, 79) = TextBox78.Text
.Cells(r, 80) = TextBox79.Text
End With
End Sub

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 3:52 pm
by snasui
:D ตัวอย่างการปรับ Code ตามด้านล่างครับ

Code: Select all

r.Offset(0, 1).Value = Application.Text(TextBox1.Text, "dd/mm/yyyy")

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 4:01 pm
by boyanny28
ขอแจ้งปัญหาเพิ่มครับ
ขออธิบายก่อนครับว่า
1.ผมทำ form มาเพื่อรับข้อมูลเพื่อนำไปวางใน Database ซึ่งใน Fileที่แนบไปคือ User form In-Put ครับ
2.ในเวลาต่อมา ผมพบว่าต้องมีการแก้ไขข้อมูล เพื่อ update สถานการณ์ในอนาคต จึงสร้าง Userform Edit ขึ้นมาครับ เพื่อแก้ไขข้อมูลครับ
3.ผมพบว่าค่าที่แสดงใน Textbox ในกลุ่มที่เป็น Date นั้น มีปัญหา ดังนี้ครับ
3.1 ใน Userform In_Put data
3.1.1 : วันที่ใน Textbox หากวันที่มากกว่าวันที่ 12 ขึ้นไป จะแสดงผลตามที่ต้องการ ยกตัวอย่างเช่น Textbox = 13/04/2014 ใน database = 13/04/2014
3.1.2 แต่หากเป็น Textbox = 10/4/2014 < dd/mm/yy > แต่พอบันทึกกลับเป็น 4/10/2014 <m/dd/yy>

3.2 ใน Userform ชื่อ Edit
ใน Userform นี้ ผมทำการ Vlookup โดยใช้ Combobox1เป็นเงื่อนไข หลังจากเลือกข้อมูลที่ Combobox1แล้ว ใน Textbox ที่เก็บข้อมูลรูปแบบวันที่ก็จะแสดงค่าตามใน Database ครับ ซึ่ง หากข้อมูลที่อยู่ใน Database เป็นไปตามเงื่อนไขข้อ 3.1.1 จะไม่พบปัญหาครับ แต่หากเป็นข้อ 3.1.2 แล้ว ค่าเวลาจะผิดไปเลยครับ เช่น ใน Textbox แสดงค่า 4/10/2014 <M/dd/yy> แต่พอเรากดปุ่มแก้ไข กลับแสดงค่า 4/10/2014 <วันที่ 4 Oct 2014> ซึ่งผิดเลยครับ
รบกวนด้วยครับ ผมไม่ทราบจริงๆว่าผมผิดตรงไหน

Re: เรื่องวันที่ใน Userform

Posted: Fri Oct 17, 2014 4:10 pm
by snasui
:D แจ้งมาด้วยว่าได้ลองตามที่ผมแนะนำไปล่าสุดแล้วยังติดปัญหาใด เพื่อจะได้แก้ไขต่อไปจากนั้นครับ