Page 1 of 1

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

Posted: Mon May 31, 2021 4:22 pm
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

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

Posted: Mon May 31, 2021 5:03 pm
by snasui
:D ที่เขียนมานั้นเรียกว่า Code ไม่ใช่สูตรครับ

แนบไฟล์โดยตัดมาเฉพาะส่วนที่เกี่ยวข้องพร้อมอธิบายว่าติดปัญหาตรงบรรทัดไหน จะได้สะดวกต่อการตอบของเพื่อนสมาชิกครับ

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

Posted: Tue Jun 01, 2021 9:26 am
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

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

Posted: Tue Jun 01, 2021 10:32 am
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

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

Posted: Tue Jun 01, 2021 11:51 am
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

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

Posted: Fri Jun 18, 2021 4:46 pm
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

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

Posted: Fri Jun 18, 2021 9:12 pm
by snasui
:D กรณีต้องใส่ค่าใด ๆ เข้าไปเพิ่มระหว่างการรัน Code จะต้องสร้าง InputBox เข้ามารับค่านั้นแล้วค่อยนำค่าจาก InputBox ไปใช้ครับ

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