Page 1 of 1

ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Thu Nov 16, 2017 8:40 pm
by kio2002
เรียนสมาชิกทุกท่านครับ
ผมต้องการให้มีการดักจับการเปลี่ยนแปลงที่เซลล์ A1
หากเซลล์ A1 ไม่เท่ากับ 7 ให้แสดง inputbox เพื่อให้ใส่ Password
ถ้าใส่ password ถูกต้อง ให้แสดง inputbox อันใหม่ เพื่อใส่ตัวเลข แล้วส่งค่าไปยังเซลล์ A1 แล้วออกจากคำสั่ง ไม่ว่าเซลล์ A1 จะไม่เท่ากับ 7 ก็ตาม
แต่ถ้ามีการเปลี่ยนแปลงตัวเลขที่เซลล์ A1อีกครั้งค่อยให้เริ่มคำสั่งใหม่ แต่ถ้าใส่ Password ผิด ก็ให้แสดง Msgbox แล้วจบคำสั่ง

ตอนนี้ติดตรงนี้ครับ (ถ้าใส่ password ถูกต้อง ให้แสดง inputbox อันใหม่ เพื่อใส่ตัวเลข แล้วส่งค่าไปยังเซลล์ A1 แล้วออกจากคำสั่ง ไม่ว่าเซลล์ A1 จะไม่เท่ากับ 7 ก็ตาม)
ผมอยากให้มันออกจากคำสั่ง แต่มันไม่ยอมออกครับ

รบกวนช่วยดู code ให้หน่อยครับ
code จะอยู่ที่ sheet1

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Message, Message1, Title, Title1, msg1, msg2 As Variant
    Message = "¡ÃسҢÍ͹حҵԨҡ ¼¨¡. áÅÐãÊè Password"
    Message1 = "ãÊèÀÒÉÕÁÙŤèÒà¾ÔèÁ"
    Title = "Password"
    Title1 = "VAT"
    Default1 = ""
        If Target.Address = Range("a1").Address Then
            If Range("a1").Value <> 7 Then
                msg1 = InputBox(Message, Title)
            If msg1 = 1234 Then
                msg2 = InputBox(Message1, Title1)
                Range("a1").Value = msg2
            Else
                MsgBox "¤Ø³ãÊè Password ¼Ô´"
                Range("a1").Value = 7
            End If
            End If
        End If
    Exit Sub
End Sub

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Thu Nov 16, 2017 9:20 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

'Other code
If Target.Address = Range("a1").Address Then
    If Range("a1").Value <> 7 Then
        msg1 = InputBox(Message, Title)
        If msg1 = 1234 Then
            Application.EnableEvents = False
            msg2 = InputBox(Message1, Title1)
            Range("a1").Value = msg2
            Application.EnableEvents = True
        Else
            MsgBox "คุณใส่ Password ผิด"
            Range("a1").Value = 7
        End If
    End If
End If
'Other code

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Thu Nov 16, 2017 9:50 pm
by kio2002
code ตรงตามที่ต้องการเลยครับอาจารย์
แค่เพิ่ม Application.EnableEvents = False และ Application.EnableEvents = True
เส้นผมบังภูเขาอีกละ
รบกวนสอบถามอาจารย์อีกหน่อยครับ
พอดีผมปรับ fromat cells เป็น Percentage เวลาส่งค่าจาก inputbox ไปยังเซลล์ A1
เช่นพิมพ์ 7 กดปุ่ม ok ค่าที่ส่งไปเป็น 700 ต้องแก้ไขตรงไหนครับอาจารย์

พอดีผมเพิ่ม "7%" ไปแล้วก็ยังไม่ได้

Code: Select all

'Other code
If Target.Address = Range("a1").Address Then
    If Range("a1").Value <> "7%" Then
        msg1 = InputBox(Message, Title)
        If msg1 = 1234 Then
            Application.EnableEvents = False
            msg2 = InputBox(Message1, Title1)
            Range("a1").Value = msg2
            Application.EnableEvents = True
        Else
            MsgBox "คุณใส่ Password ผิด"
            Range("a1").Value = "7%"
        End If
    End If
End If
'Other code

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Thu Nov 16, 2017 10:20 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

If Target.Address = Range("a1").Address Then
    If Range("a1").Value / 100 <> "7%" Then
        msg1 = InputBox(Message, Title)
        If msg1 = 1234 Then
            Application.EnableEvents = False
            msg2 = InputBox(Message1, Title1)
            Range("a1").Value = msg2 / 100
            Application.EnableEvents = True
        Else
            MsgBox "¤Ø³ãÊè Password ¼Ô´"
            Range("a1").Value = Range("a1").Value / 100
        End If
    End If
End If

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Wed Nov 22, 2017 7:59 pm
by kio2002
เรียนอาจารย์สันติพงศ์ครับ
ผมนำโค้ดไปใช้แล้วครับ แต่ยังติดปัญหาดังนี้ครับ
-พิมพ์เลข 7 ที่เซลล์ A1 แล้วยังแสดง inputbox
-ใส่ password ที่ inputbox ผิด โปรแกรมไม่ยอมออกจากคำสั่ง
รบกวนอาจารย์ดูให้อีกทีครับ
ขอบคุณครับ

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Wed Nov 22, 2017 11:28 pm
by DhitiBank
ลองแบบนี้ครับ

Code: Select all

If Target.Address = Range("a1").Address Then
    If Range("a1").Value <> 0.07 Then
        msg1 = InputBox(Message, Title)
        Application.EnableEvents = False
        If msg1 = 1234 Then
            msg2 = InputBox(Message1, Title1)
            Range("a1").Value = msg2 / 100
        Else
            MsgBox "คุณใส่ Password ผิด"
            Range("a1").Value = Range("a1").Value / 100
        End If
        Application.EnableEvents = True
    End If
End If

Re: ดักจับการเปลี่ยนแปลงที่เซลล์ A1 แล้วแสดง inputbox

Posted: Thu Nov 23, 2017 8:24 pm
by kio2002
ขอบคุณท่านอาจารย์ และสมาชิก คุณ Dhitibank เป็นอย่างมากครับ code ใช้ได้ตรงตามที่ต้องการเลยครับ
ผมปรับนิดหน่อย ตอนนี้ OK แล้วครับ
จาก
MsgBox "คุณใส่ Password ผิด"
Range("a1").Value = Range("a1").Value / 100

เป็น
MsgBox "คุณใส่ Password ผิด"
Range("a1").Value = 0.07