Excel VBA:How to use StrComp in a While statement when one string is a text value contained in a cell?
The code returning an error is the first line of the following:
While StrComp(selectedRecipe, dataSheet.Cells(i, 1)) <> 0
recipeRow = recipeRow + 1
i = i + 1
Wend
The debug I'm getting has issues with the While statement line itself. This code is contained under an OK Button click event on a userform, with selectedRecipe defined as a public string variable in the main worksheet sub. "i" is defined as an integer in this private sub. Basically the code is to find which row of the sheet holds the string value contained in selectedRecipe after selectedRecipe is selected from a drop-down combo box (selectedRecipe returns correctly and has no issues associated with it). I assume I need to have some sort of "converting" command in front of "dataSheet.Cells(i,1)" to reinforce the cell value as a string, but am not sure. Thanks!
excel vba excel-vba
add a comment |
The code returning an error is the first line of the following:
While StrComp(selectedRecipe, dataSheet.Cells(i, 1)) <> 0
recipeRow = recipeRow + 1
i = i + 1
Wend
The debug I'm getting has issues with the While statement line itself. This code is contained under an OK Button click event on a userform, with selectedRecipe defined as a public string variable in the main worksheet sub. "i" is defined as an integer in this private sub. Basically the code is to find which row of the sheet holds the string value contained in selectedRecipe after selectedRecipe is selected from a drop-down combo box (selectedRecipe returns correctly and has no issues associated with it). I assume I need to have some sort of "converting" command in front of "dataSheet.Cells(i,1)" to reinforce the cell value as a string, but am not sure. Thanks!
excel vba excel-vba
2
Ifi
is anInteger
it can overflow. It should be declared asLong
.i
also needs to be at least 1 when you enter the loop. This also begs the question why you're usingStrComp
instead of simplyWhile selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't findselectedRecipe
in column 1.
– Comintern
Nov 21 '18 at 0:13
add a comment |
The code returning an error is the first line of the following:
While StrComp(selectedRecipe, dataSheet.Cells(i, 1)) <> 0
recipeRow = recipeRow + 1
i = i + 1
Wend
The debug I'm getting has issues with the While statement line itself. This code is contained under an OK Button click event on a userform, with selectedRecipe defined as a public string variable in the main worksheet sub. "i" is defined as an integer in this private sub. Basically the code is to find which row of the sheet holds the string value contained in selectedRecipe after selectedRecipe is selected from a drop-down combo box (selectedRecipe returns correctly and has no issues associated with it). I assume I need to have some sort of "converting" command in front of "dataSheet.Cells(i,1)" to reinforce the cell value as a string, but am not sure. Thanks!
excel vba excel-vba
The code returning an error is the first line of the following:
While StrComp(selectedRecipe, dataSheet.Cells(i, 1)) <> 0
recipeRow = recipeRow + 1
i = i + 1
Wend
The debug I'm getting has issues with the While statement line itself. This code is contained under an OK Button click event on a userform, with selectedRecipe defined as a public string variable in the main worksheet sub. "i" is defined as an integer in this private sub. Basically the code is to find which row of the sheet holds the string value contained in selectedRecipe after selectedRecipe is selected from a drop-down combo box (selectedRecipe returns correctly and has no issues associated with it). I assume I need to have some sort of "converting" command in front of "dataSheet.Cells(i,1)" to reinforce the cell value as a string, but am not sure. Thanks!
excel vba excel-vba
excel vba excel-vba
asked Nov 21 '18 at 0:09
indEng123
111
111
2
Ifi
is anInteger
it can overflow. It should be declared asLong
.i
also needs to be at least 1 when you enter the loop. This also begs the question why you're usingStrComp
instead of simplyWhile selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't findselectedRecipe
in column 1.
– Comintern
Nov 21 '18 at 0:13
add a comment |
2
Ifi
is anInteger
it can overflow. It should be declared asLong
.i
also needs to be at least 1 when you enter the loop. This also begs the question why you're usingStrComp
instead of simplyWhile selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't findselectedRecipe
in column 1.
– Comintern
Nov 21 '18 at 0:13
2
2
If
i
is an Integer
it can overflow. It should be declared as Long
. i
also needs to be at least 1 when you enter the loop. This also begs the question why you're using StrComp
instead of simply While selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't find selectedRecipe
in column 1.– Comintern
Nov 21 '18 at 0:13
If
i
is an Integer
it can overflow. It should be declared as Long
. i
also needs to be at least 1 when you enter the loop. This also begs the question why you're using StrComp
instead of simply While selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't find selectedRecipe
in column 1.– Comintern
Nov 21 '18 at 0:13
add a comment |
1 Answer
1
active
oldest
votes
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
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%2f53403494%2fexcel-vbahow-to-use-strcomp-in-a-while-statement-when-one-string-is-a-text-valu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
End Sub
add a comment |
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
End Sub
add a comment |
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
End Sub
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
End Sub
answered Nov 21 '18 at 0:25
Jon Vote
3808
3808
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%2f53403494%2fexcel-vbahow-to-use-strcomp-in-a-while-statement-when-one-string-is-a-text-valu%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
2
If
i
is anInteger
it can overflow. It should be declared asLong
.i
also needs to be at least 1 when you enter the loop. This also begs the question why you're usingStrComp
instead of simplyWhile selectedRecipe <> dataSheet.Cells(i, 1).Value
. Finally, you'll run off the end of the sheet if you don't findselectedRecipe
in column 1.– Comintern
Nov 21 '18 at 0:13