Table Of Contents
Excel VBA UCase Function
Ucase in VBA is an inbuilt function which is used to convert an input string provided to it in the uppercase, it takes a single argument which is the string as an input and the output generated by this function is a string, the one thing to keep in mind is that this function converts all the function to uppercase, not just the first character.
There are situations where we need to convert some of the text values to UPPERCASE in Excel. This can be done by using an UPPER function in regular worksheet function and UCase function in VBA code.
If you are already searching for the UPPER function in VBA, you will not find it, not even with the Worksheet function class. In VBA, it is a completely different and short name function, UCASE. Here “U” stands for “UPPER,” so the formula reads “UPPERCASE.”
Syntax
Look at the syntax of the UCASE function.
String: It is nothing, but what is the text value we are trying to convert to uppercase? It could be a direct value or cell reference as well. We will see both kinds of examples in a short while.
How to Convert Text to Uppercase using VBA Ucase?
Example #1
Let us try converting the text value Excel VBA to uppercase text using the UCase function.
Step 1: Start the sub procedure by creating the Macro.
Code:
Sub UCase_Example1() End Sub
Step 2: Declare the variable as VBA String.
Code:
Sub UCase_Example1() Dim k As String End Sub
Step 3: Assign the value to the variable “k” by applying the “UCASE” function.
Step 4: Here, a string is our targeted text value that we are trying to convert to uppercase, and the string value is “excel VBA.”
Code:
Sub UCase_Example1() Dim k As String K = UCase ("excel vba") End Sub
Step 5: Let us display the result of the variable in the message box.
Code:
Sub UCase_Example1() Dim k As String k = UCase("excel vba") MsgBox k End Sub
We have completed the VBA coding part. Next, run the Macro to see the result in a message box.
So, the uppercase function converted the text value “excel VBA” to “EXCEL VBA” in seconds.
Example #2
Let us look at the example of using a cell reference to the function. The same text value we have entered in cell A1.
Step 1: We will show the result in Range B1 cell so that the code will be Range (“B”).Value =
Code:
Sub UCase_Example2() Range("B1").Value = End Sub
Step 2: In cell B1, through the UCASE function, we will store the data, so open the UCASE function.
Step 3: Here, the string value is cell reference this time. So, give the cell reference as Range (“A1”).Value.
Code:
Sub UCase_Example2() Range("B1").Value = UCase(Range("A1").Value) End Sub
So, done.
Run the code and see the result in the B1 cell.
Example #3
The above example shows only the single-cell value converted to the upper case. Imagine if you have several names like the below image.
We cannot keep writing the code for every line in these cases, so we need to enclose the formula with loops. The code below will convert the above text values to the upper case.
Code:
Sub UCase_Example3() Dim k As Long For k = 2 To 8 Cells(k, 2).Value = UCase(Cells(k, 1).Value) Next k End Sub
It will convert all the text values to the upper case from row 2 to row 8.
Imagine if you wish to convert all the selected cell values to the upper case, then use the below code.
Code:
Sub UCase_Example4() Dim Rng As Range Set Rng = Selection For Each Rng In Selection Rng = UCase(Rng.Value) Next Rng End Sub
For this code to work, first, we need to select the range of cells we wish to convert to uppercase, then run the Macro. The selected range will only convert the text values to upper case characters.