LCase Function in VBA
There are situations where we need to convert some of the text values to Lowercase characters. This can be done by using a LOWER function in regular worksheet function and LCase function in Excel VBA code. You must have tried the same formula (LOWER) as excel in VBA and you have not found it. Because in VBA Lowercase is named slightly different. In VBA it is in shortcut name i.e. “LCASE”. Here “L” stands for “LOWER”, so formula reads “LOWERCASE”.
In today’s article, we will show you another way around i.e. converting any alphabet character to LOWERCASE character.
VBA Lowercase Syntax
Now, look at the syntax of the LCASE function in VBA.
String is nothing but what is the text value we are trying to convert to Lower Case. We can supply the text directly to the formula, it can be a cell reference, and it can be through variable as well.
Examples of VBA LCase Function
Let’s see some simple to advanced examples of lowercase function in VBA to understand it better.
Excel VBA LCase – Example #1
Let’s try to convert the text value “Hello Good Morning” to lower case text by using “LCASE” function in VBA.
Step 1: Start the sub procedure by naming the macro.
Sub LCase_Example1() End Sub
Step 2: Declare the variable (k) as String.
Sub LCase_Example1() Dim k As String End Sub
Step 3: Assign the value to the variable “k” by applying “LCASE” function.
Step 4: Here string is our desired text value that we are trying to convert to lower case and the desired string value is “Hello Good Morning”.
Sub LCase_Example1() Dim k As String k = LCase("Hello Good Morning") End Sub
Step 5: Now show the result of the variable “k” in the message box.
Sub LCase_Example1() Dim k As String k = LCase("Hello Good Morning") MsgBox k End Sub
Ok, coding is done. Let’s run the code to see the result.
So LCase converted the text value “Hello Good Morning” to “hello good morning” with the simple coding technique.
Excel VBA LCase – Example #2
We have seen hoe LCASE function works in VBA. In the above example, we have directly supplied the value to the formula itself. Now we will see how can we use cell reference value to the formula.
Assume you have the word “Hello Good Morning” in the cell A1 like the below image.
Step 1: We will convert the cell A1 cell value to the lower case by showing the result in Range B1 cell, so the code will be Range (“B”).Value =
Sub LCase_Example2() Range("B1").Value End Sub
Step 2: In cell B1 through LCASE function we will store the result, so open the LCASE function.
Step 3: In this example VBA string value is a cell reference, not direct value. So give the cell reference as Range (“A1”).Value.
Sub LCase_Example2() Range("B1").Value = LCase(Range("A1").Value) End Sub
So, ok we are done with the VBA coding part. Run the code and see the magic in the B1 cell.
Excel VBA LCase – Example #3
Converting a single cell value or a single direct value isn’t the biggest challenge. When we need to deal with “n” number of values in the worksheet then we need to apply loops to loop through all the cells and convert them to lower case values.
Assume below is the data you have in an excel worksheet.
If you are not aware of loops then you need to go back to the basics of VBA coding, refer our articles on “VBA Loops” to have a fair bit of knowledge on loops. Below code will convert the above names to the lower case.
Sub LCase_Example3() Dim k As Long For k = 2 To 8 Cells(k, 2).Value = LCase(Cells(k, 1).Value) Next k End Sub
This will convert all the text values from row 2 to row 8 in the lowercase function.
Based on your cells you can increase the limit of the loop from 8 to whatever the last row number of your data.
You can download this VBA LCase Excel Template from here – VBA LCase Excel Template
This has been a guide to VBA LCase. Here we discussed how to use Excel VBA Lowercase with the help of practical examples and downloadable excel template. Below are some useful articles related to VBA –