Updated June 6, 2023
Excel VBA LCase Function
In this article, we will discuss changing the text value to lower case. We can do this by the LCase function in VBA. Similar to the UCase, LCase changes the upper case characters to lower case characters in VBA. The syntax will be as follows to use the Lcase function in VBA:
Syntax of LCase in Excel VBA
The syntax for the LCase function Excel VBA is as follows:
The string is the input we provide to the function. The input can be a single cell or a group of cells, or a whole cell range. If we use Lcase, it only changes the text to the lower case, but it does not change the structure of the text. For example, if we have a semicolon or a comma in our text, it remains untouched. For example, if we input Lcase ( EVERYTHING ), the result we will have is everything. Secondly, in an example, if we have an input such as Lcase (ANAND, ARAN / NEERAJ ), the result will be as follows: Anand, aran / Neeraj.
Let us use this function in a few examples, which will provide a better thought process about the function.
How to Use Excel VBA LCase Function?
We will learn how to use a VBA LCase with a few examples in Excel.
VBA LCase Function – Example #1
I have a string inside cell A1 of sheet 1. The string is in upper case; we will convert it to lower case using the Lcase function. Have a look at the string below.
Follow the below steps to use LCase in Excel VBA.
Step 1: We start by going to the developer’s tab to open VB Editor from a Visual Basic Option.
Step 2: Now click on the Insert tab, which will show us to insert a module in the VBA. To start writing the code, double-click on the module.
Step 3: Then declare a macro Sub Function.
Code:
Sub Sample() End Sub
Step 4: To use any of the sheet properties, we must first activate the sheet.
Code:
Sub Sample() Worksheets("Sheet1").Activate End Sub
Step 5: Now, let us change the value in cell A1 by using the LCase Function as follows.
Code:
Sub Sample() Worksheets("Sheet1").Activate Range("A1").Value = LCase(Range("A1")) End Sub
Step 6: We get the following result when we run the above code.
We changed the text from upper to lower case in the above example using the Lcase function.
VBA LCase Function – Example #2
Let us take input from the user in the Upper case and change the value of the text to lower case using the Lcase function.
Step 1: Again, we start by going to VB Editor from Visual Basic under the developer’s tab.
Step 2: Click on the Insert tab and add a new module.
Step 3: Declare a second macro different from the first one, as two macros cannot have the same name.
Code:
Sub Sample1() End Sub
Step 4: Use Dim to declare two variables, A and B, as strings.
Code:
Sub Sample1() Dim A, B As String End Sub
Step 5: Use the Inputbox function to take input from the user and store the value in variable A.
Code:
Sub Sample1() Dim A, B As String A = InputBox("Write a string", "In Uppercase") End Sub
Step 6: In the B store, the value of the Input string is changed from upper case to lower case using the Lcase function.
Code:
Sub Sample1() Dim A, B As String A = InputBox("Write a string", "In Uppercase") B = LCase(A) End Sub
Step 7: Use the Msgbox Function to display the result.
Code:
Sub Sample1() Dim A, B As String A = InputBox("Write a string", "In Uppercase") B = LCase(A) MsgBox B End Sub
Step 8: Run the above code by pressing F5, and we get a prompt to give a value. Input any string. Click on OK to see the result.
VBA LCase Function – Example #3
Above, we have discussed how this Lcase function does not affect the structure of the string, i.e., if there is a comma, a semicolon, or any other special character, it remains unaffected. I have a string in cell C1 with some special characters for demonstration. It is as below,
Step 1: Again, we start by going to VB Editor from Visual Basic under the developer’s tab.
Step 2: Click Insert and add the module above to let us write the code in the same module.
Step 3: Declare a third macro name using the Sub Function.
Code:
Sub Sample2() End Sub
Step 4: To use the string in cell C1, we need to activate sheet 1 first.
Code:
Sub Sample2() Worksheets("Sheet1").Activate End Sub
Step 5: Now, let us use the LCase function on cell C1 to check whether it affects the special characters or not.
Code:
Sub Sample2() Worksheets("Sheet1").Activate Range("c1").Value = LCase(Range("c1")) End Sub
Step 6: Execute the above code using the run button and see the following result.
We can see that Lcase does not affect any of the special characters in the string.
VBA LCase Function – Example #4
Now in this example, let us change a range of data from upper to lower case using the LCase function. For demonstration, my data is in Sheet 2, which is as follows,
I want to change the strings in column A, which are in upper case, to lower case in column B.
Step 1: Click on the Insert Tab and add the module
Step 2: Declare a macro name for this example.
Code:
Sub Sample3() End Sub
Step 3: Activate sheet 2 first to use its properties.
Code:
Sub Sample3() Worksheets("Sheet2").Activate End Sub
Step 4: Declare a variable A and its data type as long.
Code:
Sub Sample3() Worksheets("Sheet2").Activate Dim A As Long End Sub
Step 5: Now, we will use for loop to loop the next data in the next row.
Code:
Sub Sample3() Worksheets("Sheet2").Activate Dim A As Long For A = 2 To 6 End Sub
Step 6: Let us change the value in column A to lower case and store it in column B by the following code.
Code:
Sub Sample3() Worksheets("Sheet2").Activate Dim A As Long For A = 2 To 6 Cells(A, 2).Value = LCase(Cells(A, 1).Value) End Sub
Step 7: Loop until the last row is found,
Code:
Sub Sample3() Worksheets("Sheet2").Activate Dim A As Long For A = 2 To 6 Cells(A, 2).Value = LCase(Cells(A, 1).Value) Next A End Sub
Step 8: We get the following result when we run the above code.
Things to Remember
- Lcase changes text to lower case.
- The input we provide to the function can be a single cell or a range of cells.
- It remains unchanged if the input function has any special symbols or characters.
Recommended Articles
This is a guide to VBA LCase. Here we discuss how to use Excel VBA LCase Function, practical examples, and a downloadable Excel template. You can also go through our other suggested articles –