: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

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

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#1

Post by boyanny28 »

วันที่ในUserform.jpg
ผมดึงข้อมูลจาก database มาแสดงใน userform1 มาเพื่อการแก้ไข
แต่ทำไมวันที่ ที่แสดงใน Textbox ไม่แสดงวันที่ครับ แต่แสดงเป็น4xxxxครับ
ผมต้องแก้ตรงไหนครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#2

Post by snasui »

:D แจ้ง Code ที่ใช้ดึงวันที่มาด้วยครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#3

Post by boyanny28 »

ผมใช้ Vlookup ธรรมดา รบกวนดูตาม File แนบครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#4

Post by snasui »

:D ใน TextBox ข้อมูลจะกลายเป็น Text หรือ String หากต้องการให้เป็น Format วันที่ ต้องทำการกำหนดเองครับ เช่น

Code: Select all

TextBox1.Value = Format(Application.WorksheetFunction.VLookup(CLng(ComboBox1), myRange, 2, False), "dd/mm/yyyy")
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#5

Post by boyanny28 »

ขอบคุณพี่พงษ์ครับ เดี๋ยวผมขอลองก่อนครับ แล้วจะกลับมารายงานผลครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#6

Post by boyanny28 »

ได้ตามที่ต้องการแล้วครับ ขอบคุณครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#7

Post by boyanny28 »

ขอถามต่อนะครับ Format ถ้าเราต้องการเป็น ค.ศ.ต้องใช้ Format แบบไหนครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#8

Post by snasui »

:D สามารถปรับ Code เป็นด้านล่างครับ

Code: Select all

TextBox1.Text = Application.Text(Application.WorksheetFunction.VLookup(CLng(ComboBox1), myRange, 2, False), "[$- ]dd/mm/yyyy")
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#9

Post by boyanny28 »

ขอบคุณครับ แต่พอปรับโค๊ดแล้ว ใน Database ที่เป็นค่าว่างๆ แต่ใน Textbox แสดง 00/01/1900 ครับ ผมต้องศึกษาคำสั่งไหนเพิ่มเติมครับ
ถ้าผมต้องการให้ค่าว่างใน Database มาแสดงใน Textbox ก็เป็นค่าว่างเหมือนกันครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#10

Post by snasui »

:D สามารถดักด้วย If อีกรอบหลังบรรทัดเดิม เป็น

Code: Select all

if vba.left(textbox1.text,2) = "00" then textbox1.text = ""
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#11

Post by boyanny28 »

พี่ครับ
IF VBA.Left นี่คือ โค๊ดที่อยู่ด้านซ้ายใช่ไหมครับ หรือให้ผมนำโค๊ดไปวางต่อท้ายเลยครับ
ผมลองวางต่อท้าย มันไม่ได้ครับ
ถ้าจะให้เข้า IF......Then .....End if
ผมลองทำดูแล้ว พอมีข้อมูลเพิ่มเข้ามาใน cell ที่อยู่ใน database มันก็ไม่แสดงข้อมูลครับ
รบกวนด้วยครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#12

Post by boyanny28 »

ได้แล้วครับ ผมทำผิดเองครับ :D
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#13

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#14

Post by snasui »

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

ลองดูว่าในเครื่องที่ใช้งานมีการกำหนด Regional and Language ไว้แบบใดแล้วช่วยแจ้งมาด้วยครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#15

Post by boyanny28 »

Region ตามนี้ครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#16

Post by snasui »

:!: ผมลบความเห็นที่โพสต์มาเป็น Code ล่าสุดทิ้งไปเองเนื่องจากคุณโพสต์มาไม่ถูกต้อง ดูตาม Link ที่ผมให้ไปว่าทำอย่างไร คิดว่าผมทำไว้ชัดเจนดีแล้วครับ
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#17

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

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

#18

Post by snasui »

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

Code: Select all

r.Offset(0, 1).Value = Application.Text(TextBox1.Text, "dd/mm/yyyy")
boyanny28
Member
Member
Posts: 26
Joined: Wed Aug 20, 2014 7:57 pm

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

#19

Post 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> ซึ่งผิดเลยครับ
รบกวนด้วยครับ ผมไม่ทราบจริงๆว่าผมผิดตรงไหน
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#20

Post by snasui »

:D แจ้งมาด้วยว่าได้ลองตามที่ผมแนะนำไปล่าสุดแล้วยังติดปัญหาใด เพื่อจะได้แก้ไขต่อไปจากนั้นครับ
Post Reply