VBA ASC function in Excel is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel VBA. This function returns the ASCII value of the first character from a string. The ASCII stands for American Standard Code for Information Interchange. It has one string parameter. The ASC function can be used in only VBA.
We can’t use this function as a worksheet function. This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA ASC function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the ASC function, where we can use this VBA ASC function and real-time examples.
- Overview
- Syntax of VBA ASC Function
- Parameters or Arguments
- Where we can apply or use the VBA ASC Function?
- Example 1: Display an ASCII value of ‘A’
- Example 2: Display an ASCII value of ‘a’
- Example 3: Display an ASCII value of ‘WELCOME‘
- Example 4: Display an ASCII value of ‘welcome‘
- ASCII Table Chart
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA ASC Function
The syntax of the VBA ASC function is
ASC (String)
Note: This ASC function returns the ASCII code for the first character of a given string.
Parameters or Arguments
The ASC function has one string parameter or argument for the ASC Function.
Where
string: The string which you want to convert into ASCII value. If there are greater number of characters, It takes only first character of the string and converts it into the ASCII value and ignores other characters.
Where we can apply or use the VBA ASC Function?
We can use this VBA ASC function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Note: The use of VBA ASC Function is to convert a character to numeric number. For example, you may want to know ‘A to Z’ characters of ASCII values, you can by specifying a character to ASC function. Similarly, you may want to know ‘a to z’ characters if the ASCII values.
Example 1: Display an ASCII value of ‘A’
Here is a simple example of the VBA ASC function. This below example macro takes ‘A’ as input and generates output of ‘A’ ASCII value.
'Display an ASCII value of 'A' Sub VBA_ASC_Function_Ex1() Dim iOutput As Integer, sString As String sString = "A" iOutput = Asc(sString) MsgBox "The ASCII character of 'A' is :" & iOutput, vbInformation, "VBA ASC Function" End Sub
Output: The variable iOutput now contains the result as ’65’. Here is the screen shot of first example output.
Example 2: Display an ASCII value of ‘a’
Here is one more example. This below example macro takes ‘a’ as input and generates output of ‘a’ character ASCII value.
'Display an ASCII value of 'a' Sub VBA_ASC_Function_Ex2() Dim iOutput As Integer, sString As String sString = "a" iOutput = Asc(sString) MsgBox "The ASCII character of 'a' is :" & iOutput, vbInformation, "VBA ASC Function" End Sub
Output: The variable sSubString now contains the result as ’97’. Here is the screen shot of second example output.
Example 3: Display an ASCII value of ‘WELCOME’
Here is one more example. This below example macro takes ‘WELCOME’ as input and generates output of ‘W’ character ASCII value and ignores other value.
'Display an ASCII value of 'WELCOME' Sub VBA_ASC_Function_Ex3() Dim iOutput As Integer, sString As String sString = "WELCOME" iOutput = Asc(sString) MsgBox "The ASCII character of 'WELCOME' is :" & iOutput, vbInformation, "VBA ASC Function" End Sub
Output:Here is the screen shot of third example output.
Example 4: Display an ASCII value of ‘welcome’
Here is one more example. This below example macro takes ‘welcome’ as input and generates output of ‘w’ character ASCII value and ignores other value.
'Display an ASCII value of 'welcome' Sub VBA_ASC_Function_Ex4() Dim iOutput As Integer, sString As String sString = "welcome" iOutput = Asc(sString) MsgBox "The ASCII character of 'welcome' is:" & iOutput, vbInformation, "VBA ASC Function" End Sub
Output: Here is the screen shot of fourth example output.
ASCII Table Chart
Please click on below link to see ASCII Table Chart.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers