: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

บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน 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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
liveday
Bronze
Bronze
Posts: 286
Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007

บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#1

Post by liveday »

เรียนอาจารย์ทุกท่าน
ผมต้องการให้บึกทึกข้อมูลไปที่ 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
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: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#2

Post by snasui »

:D ที่เขียนมานั้นเรียกว่า Code ไม่ใช่สูตรครับ

แนบไฟล์โดยตัดมาเฉพาะส่วนที่เกี่ยวข้องพร้อมอธิบายว่าติดปัญหาตรงบรรทัดไหน จะได้สะดวกต่อการตอบของเพื่อนสมาชิกครับ
liveday
Bronze
Bronze
Posts: 286
Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007

Re: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#3

Post by liveday »

จาก 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.
User avatar
logic
Gold
Gold
Posts: 1511
Joined: Thu Mar 18, 2010 1:57 pm
Excel Ver: 365

Re: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#4

Post by logic »

แบบนี้ได้ไหมครับ

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
Bronze
Posts: 286
Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007

Re: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#5

Post by liveday »

ได้ตามต้องการครับ
ขอบคุณมากครับ และผมได้นำมาปรับเพิ่มเติมนิดนึงครับ

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
Bronze
Posts: 286
Joined: Fri Mar 25, 2011 2:24 pm
Excel Ver: 2007

Re: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#6

Post by liveday »

สอบถามข้อมูลเพิ่มครับ กรณีถ้าเราต้องการเลือก 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.
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: บันทึกหลายชีท โดยให้ถามก่อนทีละชีท

#7

Post by snasui »

:D กรณีต้องใส่ค่าใด ๆ เข้าไปเพิ่มระหว่างการรัน Code จะต้องสร้าง InputBox เข้ามารับค่านั้นแล้วค่อยนำค่าจาก InputBox ไปใช้ครับ

ศึกษาการใช้งาน InputBox ได้ที่นี่ครับ https://docs.microsoft.com/en-us/office ... n.inputbox
Post Reply