Excel VBA UCase Function
There are situations where we need to convert some of the text values to UPPERCASE in ExcelUPPERCASE In ExcelUppercase function in Excel is used to convert lowercase text to uppercase.read more. 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.”
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA UCase (wallstreetmojo.com)
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 referenceCell ReferenceCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1.read more 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.
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.”
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.
Sub UCase_Example1()
Dim k As String
k = UCase(“excel vba”)
MsgBox k
End Sub
We have completed the VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more 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 =
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.
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.
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.
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.
Recommended Articles
This article has been a guide to VBA UCase. Here, we discuss converting text to uppercase in Excel VBA, practical examples, and a downloadable Excel template. Below are some useful articles related to VBA: –
- Count Function in VBAExcel VBA ArrayListVBA EnumerationsInsert Hyperlinks with VBA