VBA Strconv Function
VBA Strconv, where Strconv stands for “String Conversion”. We all know that in VBA if we want to use text then we have to use String function to define variables. With the help of Strconv function in VBA, we can change the text to Upper case, Lower case and Proper Case without selecting the cell where it may belong. The VBA Strconv cannot be used in Excel. But in excel we can change the text of Upper case, Lower case and Proper case with already defined commands.
Syntax of StrConv in Excel VBA
The syntax for the VBA StrConv function in excel is as follows:
As per the syntax of StrConv;
- String = Set of alphabets and text in any format.
- Conversion = Type of conversion we want to apply to a selected string.
Different types of Conversions are mentioned below;
- vbUpperCase (or 1) – This option converts any type of text to Upper.
- vbLowerCase (or 2) – This option converts any type of text to Lower.
- vbProperCase (or 2) – This option converts any type of text to Proper Case, which means the first letter of each word will be in Upper case and rest letters will be in Lower case.
- vbUniCode (or 64) – This converts the text into Unicode.
- vbFromUniCode (or 128) – This converts Unicode to Default system code which can be anything.
How to Use VBA StrConv in Excel?
We will learn how to use a VBA StrConv Function with few examples in excel.
Example #1 – VBA StrConv
Step 1: Now open a new Module from Insert.
Step 2: To convert a text into different case formats we need to define 2 variables. Let’s consider our first variable Input1 as String as shown below.
Code:
Sub VBA_Strconv() Dim Input1 As String End Sub
Step 3: Now choose another variable and assign it as String. Here, our second variable is Output.
Code:
Sub VBA_Strconv() Dim Input1 As String Dim Output As String End Sub
Step 4: Now assign any text to defined variable Input1. Here we have considered a string of text “VBA string Conversion” which has the first word in Upper case, the second word in Lower case and third word in Proper case.
Code:
Sub VBA_Strconv() Dim Input1 As String Dim Output As String Input1 = "VBA string Conversion" End Sub
Step 5: Now in variable Output we will put the values stored in Input1 with the help of StrConv function as shown below.
Code:
Sub VBA_Strconv() Dim Input1 As String Dim Output As String Input1 = "VBA string Conversion" Output = StrConv( End Sub
Step 6: Now under StrConv function assign Input1 variable and the type of case we want to convert. Let’s choose vbLowerCase first for lower case text as shown below.
Code:
Sub VBA_Strconv() Dim Input1 As String Dim Output As String Input1 = "VBA string Conversion" Output = StrConv(Input1, vbLowerCase) End Sub
Step 7: Now assign the message box to variable Output to see the result of function StrConv.
Code:
Sub VBA_Strconv() Dim Input1 As String Dim Output As String Input1 = "VBA string Conversion" Output = StrConv(Input1, vbLowerCase) MsgBox Output End Sub
Step 8: Once done then compile the code and run it by clicking on the Play button located below the menu bar. We will get a message box with all the letters in Lower case as shown below.
Example #2 – VBA StrConv
In this example, we will see how vbUpperCase works for the same text which we have chosen in the above example. For this, we will continue using the same code, but we will change the conversion type only in StrConv function.
Step 1: Now add conversion type in StrConv syntax as “vbUpperCase” as shown below.
Code:
Sub VBA_Strconv2() Dim Input1 As String Dim Output As String Input1 = "VBA String Conversion" Output = StrConv(Input1, vbUpperCase) MsgBox Output End Sub
Step 2: Again compile and run the code. We will see, conversion type vbUpperCase has converted the text stored in Input1 into upper cases as shown below.
Example #3 – VBA StrConv
In this example, we will see how vbProperCase works for the same text which we have seen in the above examples. Again,
Step 1: We will choose the same code which we have seen in examples.
Code:
Sub VBA_Strconv3() Dim Input1 As String Dim Output As String Input1 = "VBA String Conversion" Output = StrConv(Input1, vbProperCase) MsgBox Output End Sub
Step 2: Now add conversion type in StrConv syntax as “vbProperCase” as shown below.
Code:
Sub VBA_Strconv3() Dim Input1 As String Dim Output As String Input1 = "VBA String Conversion" Output = StrConv(Input1, vbProperCase) MsgBox Output End Sub
Step 3: Again compile and run the code. We will see, conversion type vbProperCase has converted the text stored in Input1 into upper cases as shown below which means the first letter of all the words will be in CAPS and rest of the letters will be small.
Example #4 – VBA StrConv
In this example, we will the conversion of a string with the help of vbFromUnicode. For this again we would need a module.
Step 1: Now open a new Module from the Insert menu list and start the subcategory of VBA Strconv as shown below.
Code:
Sub VBA_Strconv4() End Sub
Step 2: In a similar way as we have seen in previous examples, we will again use same Variables Input1 and Output. Assign variable Input1 as Long.
Code:
Sub VBA_Strconv4() Dim Input1 As Long End Sub
Step 3: And assign second variable Output as Byte. This is because we will store Unicode here.
Code:
Sub VBA_Strconv4() Dim Input1 As Long Dim Output() As Byte End Sub
Step 4: Now in Output variable use Strconv function as shown below. And as per syntax, use any string. We are using the same text which we have seen above and select the conversion type as vbFromUnicode.
Code:
Sub VBA_Strconv4() Dim Input1 As Long Dim Output() As Byte Output = StrConv("VBA String Conversion", vbFromUnicode) End Sub
Step 5: Open a For-Next loop as shown below.
Code:
Sub VBA_Strconv4() Dim Input1 As Long Dim Output() As Byte Output = StrConv("VBA String Conversion", vbFromUnicode) For Next End Sub
Step 6: In For loop, give value from 0 to UBound for Output variable under Input1.
Code:
Sub VBA_Strconv4() Dim Input1 As Long Dim Output() As Byte Output = StrConv("VBA String Conversion", vbFromUnicode) For Input1 = 0 To UBound(Output) Next End Sub
Step 7: At last, to print the values stored in Input1 through for loop we will use Debug.Print as shown below.
Code:
Sub VBA_Strconv4() Dim Input1 As Long Dim Output() As Byte Output = StrConv("VBA String Conversion", vbFromUnicode) For Input1 = 0 To UBound(Output) Debug.Print Output(Input1) Next End Sub
Step 8: Now run the code. We will see, in the immediate window, the sequence of Unicode will appear randomly.
Pros of VBA Strconv
- Long set of data can easily be formatted with proper fonts with VBA Strconv.
- For maintaining a database where we need to keep proper fonts which are mostly used, we can choose VBA Strconv to convert fonts in any format.
Things to Remember
- It can only be used for texts.
- This process is similar to the insert function of Excel where we can use Upper, Lower and Proper function to convert the fonts.
- It can be automated by recording a macro. That will give the same results as VBA Strconv.
- Save the file in Macro Enable Excel, so that you would be able to see and edit the code in the future.
- We can use String or Long to define variables. Both can be used for text.
Recommended Articles
This is a guide to VBA StrConv. Here we discuss how to use Excel VBA StrConv Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –