Fix If statment to accept Upper or Lower Case

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Macro will not work if input box entry is in upper case and If statment = a item in lower cas
Is there a way to fix so macro will work regardless of how entered in input bo
Thank
Bob Leonar

Sub TestIf(

Dim varInput As Strin
Range("P5").Selec
varInput = InputBox("Enter Value"
Selection.VALUE = varInpu

If Selection.VALUE = "test_1" The
Application.Run "test

ElseIf Selection.VALUE = "test_2" Then
Application.Run "test2

End I
End Su
 
Bob,

At the very top of the code module, above and outside of any
procedures, put the line

Option Compare Text


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



rleonard said:
Macro will not work if input box entry is in upper case and If
statment = a item in lower case
 
I don't think it's the macro that's causing the error.

if lcase(selection.value) = "test_1" then
should work ok.

or this too:
if lcase(varinput) = "test_1" then


It's the text comparison that's causing the trouble.

If you always want to ignore upper/lower case differences, you can add:

Option Compare Text

at the top of the module.
 
Typical solution is to force both sides of the comparison to all upper case
or all lower case.

If LCase(varInput) = LCase("test_1") Then

Troy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top