VBA UCase

Publication Date :

Blog Author :

Table Of Contents

arrow

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.”

VBA UCase

Syntax

Look at the syntax of the UCASE function.

syntax

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
Create Subprocedure

Step 2: Declare the variable as VBA String.

Code:

Sub UCase_Example1()

Dim k As String

End Sub
Declare Varaiable - 1.2

Step 3: Assign the value to the variable “k” by applying the “UCASE” function.

Open Function 1.3

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
Excel vba ucase example 1.4

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
vba uppercase example 1.5

We have completed the VBA coding part. Next, run the Macro to see the result in a message box.

vba uppercase example 1.6

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.

vba uppercase example 2.1

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
vba uppercase example 2.2

Step 2: In cell B1, through the UCASE function, we will store the data, so open the UCASE function.

vba uppercase example 2.3

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
uppercase example 2.4

So, done.

Run the code and see the result in the B1 cell.

uppercase example 2.5

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.

UCase Example 3.1

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
UCase Example 3.2

It will convert all the text values to the upper case from row 2 to row 8.

Ucase - 3.3

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
Ucase Example 3.4

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.