Do while loop not stopping
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
|
show 1 more comment
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
What is the point of theWith
?
– John Coleman
Nov 20 '18 at 16:33
Please removeOr product2 <> "p"
in the ` Do While`
– Hiten004
Nov 20 '18 at 16:35
You are using<>
Not Equal To. You want anAND
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 putExit Do
afterMsgBox "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
|
show 1 more comment
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
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
excel vba excel-vba
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 theWith
?
– John Coleman
Nov 20 '18 at 16:33
Please removeOr product2 <> "p"
in the ` Do While`
– Hiten004
Nov 20 '18 at 16:35
You are using<>
Not Equal To. You want anAND
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 putExit Do
afterMsgBox "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
|
show 1 more comment
What is the point of theWith
?
– John Coleman
Nov 20 '18 at 16:33
Please removeOr product2 <> "p"
in the ` Do While`
– Hiten004
Nov 20 '18 at 16:35
You are using<>
Not Equal To. You want anAND
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 putExit Do
afterMsgBox "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
|
show 1 more comment
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 20 '18 at 16:36
Scott Holtzman
22.9k62748
22.9k62748
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 anAND
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
afterMsgBox "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