Drop-down list + VBA, issues with uppercase letters
Solved
Anthelm
Posted messages
202
Status
Member
-
Yoyo01000 Posted messages 1720 Registration date Status Member Last intervention -
Yoyo01000 Posted messages 1720 Registration date Status Member Last intervention -
Hello,
I have a dropdown list with "Yes" and "No" as choices.
I have the following code:
If Worksheets("Options").Range("B2") = ("Yes") Then
option1 = "yes"
When the user clicks "Yes" in the list, I get a capital letter, so there is no problem.
But if they type "yes" on the keyboard, the cell accepts it, but the macro (which considers that "yes" is not equal to "Yes") does not.
I thought about If Worksheets("Options").Range("B2") = ("Yes") OR (the same thing) = ("yes") Then (etc etc)
It would work if the user entered "yes" with or without a capital letter.
But that's a bit silly because someone could always type "yeS" or "yEs" due to a typo... I exaggerate a bit, but it's not optimal in itself. Especially since the user won't realize that the result in VBA won't return the right thing!
In your opinion, what should be reviewed?
1) Is the dropdown list a bad choice (maybe a checkbox would be better, for example?)
2) Is the macro too simple, and with another coding approach, can we avoid this "case sensitivity"?
Thank you for giving me a lead if you have experienced a similar situation...
Have a great day everyone!
I have a dropdown list with "Yes" and "No" as choices.
I have the following code:
If Worksheets("Options").Range("B2") = ("Yes") Then
option1 = "yes"
When the user clicks "Yes" in the list, I get a capital letter, so there is no problem.
But if they type "yes" on the keyboard, the cell accepts it, but the macro (which considers that "yes" is not equal to "Yes") does not.
I thought about If Worksheets("Options").Range("B2") = ("Yes") OR (the same thing) = ("yes") Then (etc etc)
It would work if the user entered "yes" with or without a capital letter.
But that's a bit silly because someone could always type "yeS" or "yEs" due to a typo... I exaggerate a bit, but it's not optimal in itself. Especially since the user won't realize that the result in VBA won't return the right thing!
In your opinion, what should be reviewed?
1) Is the dropdown list a bad choice (maybe a checkbox would be better, for example?)
2) Is the macro too simple, and with another coding approach, can we avoid this "case sensitivity"?
Thank you for giving me a lead if you have experienced a similar situation...
Have a great day everyone!
3 answers
Ok, the solution is with UCASE in my opinion.
This converts the result to uppercase.
If Worksheets("Options").Range("B2") = ("Yes")
the test is if = "Yes"
So if "yes", the result is false because of the missing uppercase.
But if we do:
If UCase(Worksheets("Options").Range("B2")) = ("YES")
In other words, if the result in uppercase = YES
No matter what the user enters: "yEs" becomes "YES" and it is equal to "YES", the result is true.
This converts the result to uppercase.
If Worksheets("Options").Range("B2") = ("Yes")
the test is if = "Yes"
So if "yes", the result is false because of the missing uppercase.
But if we do:
If UCase(Worksheets("Options").Range("B2")) = ("YES")
In other words, if the result in uppercase = YES
No matter what the user enters: "yEs" becomes "YES" and it is equal to "YES", the result is true.