VBA Tip: Uppercase and Lowercase
To convert a string to uppercase, use the UCase function:
Sub test()
MsgBox UCase("test 1") 'Returns TEST 1
MsgBox UCase("Test 2") 'Returns TEST 2
MsgBox UCase("TEST 3") 'Returns TEST 3
MsgBox UCase("TeSt 4") 'Returns TEST 4
End Sub
To convert a string to lowercase, use the LCase function:
Sub test()
MsgBox LCase("test 1") 'Returns test 1
MsgBox LCase("Test 2") 'Returns test 2
MsgBox LCase("TEST 3") 'Returns test 3
MsgBox LCase("TeSt 4") 'Returns test 4
End Sub
Tip: check if a value is uppercase (or lowercase)
To check if a value is all uppercase, you can do so easily by checking if the value is equal to the same uppercase value (by using the UCase function).
Sub test()
myValue = "HelLO"
If myValue = UCase(myValue) Then 'Test if uppercase
MsgBox "Yes, myValue is in caps."
Else
MsgBox "No, myValue is not in caps." '<= Returns this value
End If
End Sub
In this example the "HelLO" value is not all caps, so it fails the myValue = UCase(myValue)
test.
Sub test()
myValue = "HELLO 1234"
If myValue = UCase(myValue) Then 'Test if uppercase
MsgBox "Yes, myValue is in caps."'<= Returns this value
Else
MsgBox "No, myValue is not in caps."
End If
End Sub
In this second example, the "HELLO 1234" value does not contain any lowercase letters, so it passes the myValue = UCase(myValue)
test.
On the other hand, to check if a value is all lowercase, simply replace UCase with LCase.