Do while loop not stopping












0














I have the below do while loop that is not exiting the loop when I want it to. I simply want the loop to end once either a P or p is entered or continue if either of those options is not entered. What am I missing? Thanks!



Private Sub btnDoWhile_Click()

Dim product2 As String

With wsLoops

Do While product2 <> "P" Or product2 <> "p"
product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
If product2 = "P" Or product2 = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If
Loop

End With

End Sub









share|improve this question
























  • What is the point of the With?
    – John Coleman
    Nov 20 '18 at 16:33










  • Please remove Or product2 <> "p" in the ` Do While`
    – Hiten004
    Nov 20 '18 at 16:35










  • You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
    – JNevill
    Nov 20 '18 at 16:35












  • You could put Exit Do after MsgBox "Thank you"
    – John Coleman
    Nov 20 '18 at 16:36












  • Thank you! The and was what I was missing
    – financedude
    Nov 20 '18 at 16:39
















0














I have the below do while loop that is not exiting the loop when I want it to. I simply want the loop to end once either a P or p is entered or continue if either of those options is not entered. What am I missing? Thanks!



Private Sub btnDoWhile_Click()

Dim product2 As String

With wsLoops

Do While product2 <> "P" Or product2 <> "p"
product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
If product2 = "P" Or product2 = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If
Loop

End With

End Sub









share|improve this question
























  • What is the point of the With?
    – John Coleman
    Nov 20 '18 at 16:33










  • Please remove Or product2 <> "p" in the ` Do While`
    – Hiten004
    Nov 20 '18 at 16:35










  • You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
    – JNevill
    Nov 20 '18 at 16:35












  • You could put Exit Do after MsgBox "Thank you"
    – John Coleman
    Nov 20 '18 at 16:36












  • Thank you! The and was what I was missing
    – financedude
    Nov 20 '18 at 16:39














0












0








0







I have the below do while loop that is not exiting the loop when I want it to. I simply want the loop to end once either a P or p is entered or continue if either of those options is not entered. What am I missing? Thanks!



Private Sub btnDoWhile_Click()

Dim product2 As String

With wsLoops

Do While product2 <> "P" Or product2 <> "p"
product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
If product2 = "P" Or product2 = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If
Loop

End With

End Sub









share|improve this question















I have the below do while loop that is not exiting the loop when I want it to. I simply want the loop to end once either a P or p is entered or continue if either of those options is not entered. What am I missing? Thanks!



Private Sub btnDoWhile_Click()

Dim product2 As String

With wsLoops

Do While product2 <> "P" Or product2 <> "p"
product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
If product2 = "P" Or product2 = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If
Loop

End With

End Sub






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:23









EvR

1,2342313




1,2342313










asked Nov 20 '18 at 16:29









financedude

71




71












  • What is the point of the With?
    – John Coleman
    Nov 20 '18 at 16:33










  • Please remove Or product2 <> "p" in the ` Do While`
    – Hiten004
    Nov 20 '18 at 16:35










  • You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
    – JNevill
    Nov 20 '18 at 16:35












  • You could put Exit Do after MsgBox "Thank you"
    – John Coleman
    Nov 20 '18 at 16:36












  • Thank you! The and was what I was missing
    – financedude
    Nov 20 '18 at 16:39


















  • What is the point of the With?
    – John Coleman
    Nov 20 '18 at 16:33










  • Please remove Or product2 <> "p" in the ` Do While`
    – Hiten004
    Nov 20 '18 at 16:35










  • You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
    – JNevill
    Nov 20 '18 at 16:35












  • You could put Exit Do after MsgBox "Thank you"
    – John Coleman
    Nov 20 '18 at 16:36












  • Thank you! The and was what I was missing
    – financedude
    Nov 20 '18 at 16:39
















What is the point of the With?
– John Coleman
Nov 20 '18 at 16:33




What is the point of the With?
– John Coleman
Nov 20 '18 at 16:33












Please remove Or product2 <> "p" in the ` Do While`
– Hiten004
Nov 20 '18 at 16:35




Please remove Or product2 <> "p" in the ` Do While`
– Hiten004
Nov 20 '18 at 16:35












You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
– JNevill
Nov 20 '18 at 16:35






You are using <> Not Equal To. You want an AND here. product2 <> "P" AND product2 <> "p" Otherwise this will always pass because it can never be both "P" and "p".
– JNevill
Nov 20 '18 at 16:35














You could put Exit Do after MsgBox "Thank you"
– John Coleman
Nov 20 '18 at 16:36






You could put Exit Do after MsgBox "Thank you"
– John Coleman
Nov 20 '18 at 16:36














Thank you! The and was what I was missing
– financedude
Nov 20 '18 at 16:39




Thank you! The and was what I was missing
– financedude
Nov 20 '18 at 16:39












2 Answers
2






active

oldest

votes


















0














Here it is ..... possible solution



Private Sub btnDoWhile_Click()
Dim product2 As String

With wsLoops
Do
product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
If product2 = "P" Or product2 = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If
Loop While Not product2 = "P" And Not product2 = "p"
End With

End Sub





share|improve this answer





















  • Thanks! I've never tried using while not but it makes a lot of sense
    – financedude
    Nov 20 '18 at 16:54










  • @financedude, my pleasure.
    – Guest
    Nov 20 '18 at 16:56



















2














You can simplify the logic in this way (as well as others).



Private Sub btnDoWhile_Click()

Dim product2 As String

Do

product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)

If LCase$(product2) = "p" Then
MsgBox "Thank you"
ElseIf product2 = "" Then
Exit Sub
Else
MsgBox "Please enter a valid product number"
End If

Loop Until LCase$(product2) = "p"

End Sub





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397400%2fdo-while-loop-not-stopping%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here it is ..... possible solution



    Private Sub btnDoWhile_Click()
    Dim product2 As String

    With wsLoops
    Do
    product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
    If product2 = "P" Or product2 = "p" Then
    MsgBox "Thank you"
    ElseIf product2 = "" Then
    Exit Sub
    Else
    MsgBox "Please enter a valid product number"
    End If
    Loop While Not product2 = "P" And Not product2 = "p"
    End With

    End Sub





    share|improve this answer





















    • Thanks! I've never tried using while not but it makes a lot of sense
      – financedude
      Nov 20 '18 at 16:54










    • @financedude, my pleasure.
      – Guest
      Nov 20 '18 at 16:56
















    0














    Here it is ..... possible solution



    Private Sub btnDoWhile_Click()
    Dim product2 As String

    With wsLoops
    Do
    product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
    If product2 = "P" Or product2 = "p" Then
    MsgBox "Thank you"
    ElseIf product2 = "" Then
    Exit Sub
    Else
    MsgBox "Please enter a valid product number"
    End If
    Loop While Not product2 = "P" And Not product2 = "p"
    End With

    End Sub





    share|improve this answer





















    • Thanks! I've never tried using while not but it makes a lot of sense
      – financedude
      Nov 20 '18 at 16:54










    • @financedude, my pleasure.
      – Guest
      Nov 20 '18 at 16:56














    0












    0








    0






    Here it is ..... possible solution



    Private Sub btnDoWhile_Click()
    Dim product2 As String

    With wsLoops
    Do
    product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
    If product2 = "P" Or product2 = "p" Then
    MsgBox "Thank you"
    ElseIf product2 = "" Then
    Exit Sub
    Else
    MsgBox "Please enter a valid product number"
    End If
    Loop While Not product2 = "P" And Not product2 = "p"
    End With

    End Sub





    share|improve this answer












    Here it is ..... possible solution



    Private Sub btnDoWhile_Click()
    Dim product2 As String

    With wsLoops
    Do
    product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)
    If product2 = "P" Or product2 = "p" Then
    MsgBox "Thank you"
    ElseIf product2 = "" Then
    Exit Sub
    Else
    MsgBox "Please enter a valid product number"
    End If
    Loop While Not product2 = "P" And Not product2 = "p"
    End With

    End Sub






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 16:41









    Guest

    2624




    2624












    • Thanks! I've never tried using while not but it makes a lot of sense
      – financedude
      Nov 20 '18 at 16:54










    • @financedude, my pleasure.
      – Guest
      Nov 20 '18 at 16:56


















    • Thanks! I've never tried using while not but it makes a lot of sense
      – financedude
      Nov 20 '18 at 16:54










    • @financedude, my pleasure.
      – Guest
      Nov 20 '18 at 16:56
















    Thanks! I've never tried using while not but it makes a lot of sense
    – financedude
    Nov 20 '18 at 16:54




    Thanks! I've never tried using while not but it makes a lot of sense
    – financedude
    Nov 20 '18 at 16:54












    @financedude, my pleasure.
    – Guest
    Nov 20 '18 at 16:56




    @financedude, my pleasure.
    – Guest
    Nov 20 '18 at 16:56













    2














    You can simplify the logic in this way (as well as others).



    Private Sub btnDoWhile_Click()

    Dim product2 As String

    Do

    product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)

    If LCase$(product2) = "p" Then
    MsgBox "Thank you"
    ElseIf product2 = "" Then
    Exit Sub
    Else
    MsgBox "Please enter a valid product number"
    End If

    Loop Until LCase$(product2) = "p"

    End Sub





    share|improve this answer


























      2














      You can simplify the logic in this way (as well as others).



      Private Sub btnDoWhile_Click()

      Dim product2 As String

      Do

      product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)

      If LCase$(product2) = "p" Then
      MsgBox "Thank you"
      ElseIf product2 = "" Then
      Exit Sub
      Else
      MsgBox "Please enter a valid product number"
      End If

      Loop Until LCase$(product2) = "p"

      End Sub





      share|improve this answer
























        2












        2








        2






        You can simplify the logic in this way (as well as others).



        Private Sub btnDoWhile_Click()

        Dim product2 As String

        Do

        product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)

        If LCase$(product2) = "p" Then
        MsgBox "Thank you"
        ElseIf product2 = "" Then
        Exit Sub
        Else
        MsgBox "Please enter a valid product number"
        End If

        Loop Until LCase$(product2) = "p"

        End Sub





        share|improve this answer












        You can simplify the logic in this way (as well as others).



        Private Sub btnDoWhile_Click()

        Dim product2 As String

        Do

        product2 = Left(InputBox("Please enter product number", "Product please", "Enter product number here"), 1)

        If LCase$(product2) = "p" Then
        MsgBox "Thank you"
        ElseIf product2 = "" Then
        Exit Sub
        Else
        MsgBox "Please enter a valid product number"
        End If

        Loop Until LCase$(product2) = "p"

        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 16:36









        Scott Holtzman

        22.9k62748




        22.9k62748






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397400%2fdo-while-loop-not-stopping%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga