VBA ASC Function

VBA ASC Function

Objective of VBA ASC Function

VBA ASC function 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 can be used 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.
Table of Contents

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.
VBA ASC Function

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.
VBA ASC Function

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.
VBA ASC Function

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.
VBA ASC Function

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA ASC function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

ASCII Table Chart

Please click on below link to see ASCII Table Chart.

ASCII Table

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *