snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
liveday
Bronze
Posts: 286 Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007
#1
Post
by liveday » Mon May 31, 2021 4:22 pm
เรียนอาจารย์ทุกท่าน
ผมต้องการให้บึกทึกข้อมูลไปที่ Sheet ที่ต้องการ โดยให้ถามก่อนจะบันทึกครับ ตามสูตรที่ส่งให้จะบันทึกทีเดียวเลย 2 ชีท
ผมอยากให้ถามก่อนบันทึกในแต่ละ Sheet ครับ และสามารถยกเลิก Sheet ใด Sheet หนึ่งได้ครับ จะต้องปรับสูตรอย่างไรครับ
Code: Select all
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
AnswerYes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 1 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
With ThisWorkbook.Worksheets("Page1")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox1.Text
.Range("O28").Value = TextBox2.Text
.Range("O30").Value = TextBox3.Text
.Range("O32").Value = TextBox4.Text
.Range("AF32").Value = TextBox5.Text
.Range("AU30").Value = TextBox8.Text
.Range("AP24").Value = TextBox6.Text
.Range("AP27").Value = TextBox7.Text
' MsgBox "ใช้ข้อมูลนี้"
'Else
AnswerYes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 2 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
With ThisWorkbook.Worksheets("Page2")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox1.Text
.Range("O28").Value = TextBox2.Text
.Range("O30").Value = TextBox3.Text
.Range("O32").Value = TextBox4.Text
.Range("AF32").Value = TextBox5.Text
.Range("AU30").Value = TextBox8.Text
.Range("AP24").Value = TextBox6.Text
.Range("AP27").Value = TextBox7.Text
' MsgBox "ใช้ข้อมูลนี้"
If ComboBox1 = "" Then
MsgBox "ไม่มีข้อมูล"
Exit Sub
End If
End With
End With
Call ClearText
Application.ScreenUpdating = True
Else
' MsgBox "ยกเลิก"
End If
End Sub
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#2
Post
by snasui » Mon May 31, 2021 5:03 pm
ที่เขียนมานั้นเรียกว่า Code ไม่ใช่สูตรครับ
แนบไฟล์โดยตัดมาเฉพาะส่วนที่เกี่ยวข้องพร้อมอธิบายว่าติดปัญหาตรงบรรทัดไหน จะได้สะดวกต่อการตอบของเพื่อนสมาชิกครับ
liveday
Bronze
Posts: 286 Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007
#3
Post
by liveday » Tue Jun 01, 2021 9:26 am
จาก Code ด้านล่างนี้นะครับ ที่ Sheet1 UserForm (Customer) ผมต้องการสามารถเลือกได้ว่าจะ Paste ลงใน Sheet ไหนก็ได้ที่กำหนดนะครับ โดยการถามก่อนทุกครั้ง ครับ
Code: Select all
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
AnswerYes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 1 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
With ThisWorkbook.Worksheets("Page1")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
' MsgBox "ใช้ข้อมูลนี้"
'Else
End With
AnswerYes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 2 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
With ThisWorkbook.Worksheets("Page2")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
AnswerYes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 3 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
With ThisWorkbook.Worksheets("Page3")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
' MsgBox "ใช้ข้อมูลนี้"
If ComboBox1 = "" Then
MsgBox "ไม่มีข้อมูล"
Exit Sub
End If
End With
End With
Call ClearText
Application.ScreenUpdating = True
Else
' MsgBox "ยกเลิก"
End If
End Sub
You do not have the required permissions to view the files attached to this post.
logic
Gold
Posts: 1511 Joined: Thu Mar 18, 2010 1:57 pm
Excel Ver: 365
#4
Post
by logic » Tue Jun 01, 2021 10:32 am
แบบนี้ได้ไหมครับ
Code: Select all
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 1 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Then
With ThisWorkbook.Worksheets("Page1")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
' MsgBox "ใช้ข้อมูลนี้"
'Else
End With
End If
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 2 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbNo Then
With ThisWorkbook.Worksheets("Page2")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
End With
End If
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 3 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Then
With ThisWorkbook.Worksheets("Page3")
.Range("H19").Value = ComboBox1.Text
.Range("H24").Value = TextBox2.Text
.Range("O28").Value = TextBox3.Text
.Range("O30").Value = TextBox4.Text
.Range("O32").Value = TextBox5.Text
.Range("AF32").Value = TextBox6.Text
.Range("AU30").Value = TextBox1.Text
.Range("AP24").Value = TextBox7.Text
.Range("AP27").Value = TextBox8.Text
' MsgBox "ใช้ข้อมูลนี้"
End With
End If
If ComboBox1 = "" Then
MsgBox "ไม่มีข้อมูล"
Exit Sub
End If
' End With
' End With
Call ClearText
Application.ScreenUpdating = True
' Else
' MsgBox "ยกเลิก"
' End If
'End With
End Sub
liveday
Bronze
Posts: 286 Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007
#5
Post
by liveday » Tue Jun 01, 2021 11:51 am
ได้ตามต้องการครับ
ขอบคุณมากครับ และผมได้นำมาปรับเพิ่มเติมนิดนึงครับ
Code: Select all
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 123 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Then
Call Page1
Call Page2
Call Page3
End If
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 1 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Then
Call Page1
End If
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 2 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Then
Call Page2
End If
answeryes = MsgBox("ต้องการใช้ข้อมูลลูกค้านี้สำหรับ Page 3 ใช่หรือ ไม่", vbQuestion + vbYesNo, "User Repsonse")
If answeryes = vbYes Thenn
Call Page3
End If
Call ClearText
Application.ScreenUpdating = True
End Sub
liveday
Bronze
Posts: 286 Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007
#6
Post
by liveday » Fri Jun 18, 2021 4:46 pm
สอบถามข้อมูลเพิ่มครับ กรณีถ้าเราต้องการเลือก answeryes = vbYes Then แล้ว เมื่อถัดไป ต้องการใส่ข้อมูลเพิ่ม จะต้องแก้ Code อย่างไรครับ
กรณีนี้ Code ดัวกล่าว เมื่อกด yes Page1 แล้ว พอไป Page2 จะใส่เงินมัดเพิ่มจะใส่จำนวนเงินใหม่ไม่ได้ครับ หรือถ้าเลือก No Userform จะหายไปครับทำให้ต้องเริ่มต้นใหม่เลย ต้องใช้ Code อย่างไรเพื่อให้ใส่ได้ทีละ Page หรือเลือก Pageที่จะใส่เงินมัดจำได้ครับ
Code: Select all
Private Sub CommandButton1_Click()
Dim Answer As Variant
Application.ScreenUpdating = False
AnswerYes = MsgBox("Deposit Page1, vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
Call Page1
Call DepositPage2
AnswerYes = MsgBox("Deposit Page2", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
Call Page2
Call DepositPage3
AnswerYes = MsgBox("Deposit Page3", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
Call Page3
End If
End If
End If
If AnswerYes = vbNo Then
End If
Call ClearText
Application.ScreenUpdating = True
Me.Hide
End Sub
Sub ClearText()
TextBox1.Text = "0.00"
TextBox2.Text = "0.00"
TextBox3.Text = "0.00"
End Sub
Sub Page1()
With ThisWorkbook.Worksheets("Price1")
.Range("I15").Value = TextBox2.Text 'Deposit
End With
End Sub
Sub Page2()
With ThisWorkbook.Worksheets("Price2")
.Range("I15").Value = TextBox2.Text 'Deposit
End With
End Sub
Sub Page3()
With ThisWorkbook.Worksheets("Price3")
.Range("I15").Value = TextBox2.Text 'Deposit
End With
End Sub
Private Sub UserForm_Activate()
Call DepositPage1
TextBox1 = Format(TextBox1, "#,##0.00")
TextBox2 = Format(TextBox2, "#,##0.00")
TextBox3 = Format(TextBox3, "#,##0.00")
' TextBox1 = Format(TextBox1, "#,##0.00")
' TextBox3.Value = Format(TextBox3.Value, "#,##0.00")
' TextBox2 = Format(TextBox2, "#,##0.00")
End Sub
Sub DepositPage1()
Price.TextBox1 = Sheets("Price1").Range("I14")
TextBox2.Value = Val(TextBox1.Value) * 0.4
TextBox3.Value = Val(TextBox1.Value - TextBox2.Value)
End Sub
Sub DepositPage2()
Price.TextBox1 = Sheets("Price2").Range("I14")
TextBox2.Value = Val(TextBox1.Value) * 0.4
TextBox3.Value = Val(TextBox1.Value - TextBox2.Value)
End Sub
Sub DepositPage3()
Price.TextBox1 = Sheets("Price3").Range("I14")
TextBox2.Value = Val(TextBox1.Value) * 0.4
TextBox3.Value = Val(TextBox1.Value - TextBox2.Value)
End Sub
Private Sub sum_boxes()
TextBox3.Value = (TextBox1.Value + TextBox2.Value)
End Sub
Private Sub TextBox2_AfterUpdate()
TextBox3.Value = Val(TextBox1.Value - TextBox2.Value)
TextBox2.Value = Format(TextBox2.Value, "#,##0.00")
TextBox3.Value = Format(TextBox3.Value, "#,##0.00")
End Sub
Private Sub TextBox2_Enter()
TextBox3.Value = Val(TextBox1.Value - TextBox2.Value)
TextBox3.Value = Format(TextBox3.Value, "#,##0.00")
End Sub
You do not have the required permissions to view the files attached to this post.