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()
my_value = "HelLO"
If my_value = UCase(my_value) Then 'Test if uppercase
MsgBox "Yes, my_value is in caps."
Else
MsgBox "No, my_value 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 my_value = UCase(my_value)
test.
Sub test()
my_value = "HELLO 1234"
If my_value = UCase(my_value) Then 'Test if uppercase
MsgBox "Yes, my_value is in caps."'<= Returns this value
Else
MsgBox "No, my_value 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 my_value = UCase(my_value)
test.
On the other hand, to check if a value is all lowercase, simply replace UCase with LCase.