How to use Excel VBA ASC Function?

Excel VBA’s ASC function returns the ASCII code of a character, which can be useful for tasks such as sorting and comparing strings. In this tutorial, we’ll cover how to use the ASC function in Excel VBA  with syntax and examples.

This Tutorial Covers:

  1. What is Excel VBA ASC Function
    • Syntax
    • Parameters or Arguments
  2. Importance of Excel VBA ASC Function
  3. Examples of Excel VBA ASC
    • Example #1
    • Example #2
  4. Things to Remember
  1. What is Excel VBA ASC Function?

The ASC function in VBA produces an integer value that corresponds to the first character of a supplied string (string provided as argument/parameter), which is represented by a character code. It is often entered using the Visual Basic Editor and can be used or provided in a macro code.

The Visual Basic Editor (VBE), which executes the macro in Excel, can be used to alter and debug the macro programs. The Excel workbook is linked to it and the macro code is stored there.

The ASCII character code for the returned integer by the ASC function in VBA. There are 128 defined characters in ASCII (American Standard Code for Information Interchange), a 7-bit character encoding that includes the Latin alphabet, 10 Arabic numbers, some punctuation, and control characters. Diacritical characters are not included since their encoding needs an 8-bit minimum. With ANSI (American National Standards Institute), which contains 256 designated characters, we can carry out this 8-bit coding. Extended ASCII is another name for ANSI.

  • Syntax:

Microsoft Excel’s ASC function has the following syntax:

ASC(text)

Excel VBA Asc Function

  • Parameters or Arguments:

The ASC function in Excel VBA takes only one argument:

text: The ASCII code must be found for the string expression or single character expression represented by this parameter, which is a requirement. The function returns the ASCII code of the string’s first character if the string expression contains more than one character. A runtime error happens if the string expression is empty or has no characters.

  1. Importance of Excel VBA ASC Function:

The ASC function in Excel VBA is important for a number of reasons:

  1. It allows you to work with ASCII character codes in your VBA code. This can be useful when you need to compare or manipulate characters in a string.
  2. The ASC function is a built-in function in VBA, which means that you don’t need to write your own custom function to perform this task.
  3. The ASC function is easy to use and can be incorporated into your VBA code quickly and easily.
  4. It can help you to automate repetitive tasks in Excel that involve working with text data.
  5. The ASC function can also be combined with other VBA functions and statements to perform more complex text processing tasks.

Overall, the ASC function is an important tool in the VBA programmer’s arsenal and understanding how it works and how to use it effectively can help you to become more productive and efficient when working with text data in Excel.

  1. Examples of Excel VBA ASC:

The only way to use the ASC function in Microsoft Excel is via VBA code. See how the Asc can be used in Excel VBA in the example below.

  • Example #1:

We have certain strings in an Excel file. With these strings, we want to employ the Asc function. Let’s first look at the strings in the file, though:

Excel VBA Asc Function

You can use this Asc function in in the Visual Basic Editor (VBE) within Excel. Here are the steps to follow:

Step 1: To start the VBE, press Alt + F11. To create a new module, right-click on the project name in the VBE, select “Insert,” and then click “Module.”

Excel VBA Asc Function

Step 2: Copy or type the below code into the new module.

Sub Find_ASCII_Code()

Dim character As String

Dim ascii_code As Integer

character = Range(“A2”).Value

ascii_code = Asc(character)

MsgBox “The ASCII code of ” & character & ” is ” & ascii_code

End Sub

Excel VBA Asc Function

Step 3: Press F5 to launch the module. When the macro is executed, the value “67” is returned in a message box, as shown in the screenshot below. It suggests that “C”‘s character code is 67.

Excel VBA Asc Function

Step 4: If you want to know the ASCII code of cell A3 then, you can change the cell name in the code like below:

Excel VBA Asc Function

Step 5: Again, press F5 to launch the module. When the macro is executed, the value “69” is returned in a message box, as shown in the screenshot below. It suggests that “E”‘s character code is 69.

Excel VBA Asc Function

The following character codes appear in the message box when each of these sub-procedures is conducted separately. (one at a time, respectively). It is the list of corresponding values that were given for the strings mentioned above.

Excel VBA Asc Function

  • Example #2:

The VBA ASC function gives a run-time error if the string provided as a parameter or argument is an empty/blank string (or a string with no characters).

You can use this Asc function in in the Visual Basic Editor (VBE) within Excel. Here are the steps to follow:

Step 1: To start the VBE, press Alt + F11. To create a new module, right-click on the project name in the VBE, select “Insert,” and then click “Module.”

Excel VBA Asc Function

Step 2: Copy or paste the below code into the new module:

Sub blank()

Dim result

result = Asc(“”)

MsgBox (result)

End Sub

Excel VBA Asc Function

Step 3: Press F5 to launch the module. When the macro is executed, the following run-time error is returned.

Excel VBA Asc Function

The function gives a run-time error when the string provided as a parameter or argument to the ASC function is blank or empty, as can be seen in the screenshot above.

4. Things to Remember:

  • Any legitimate string expression may be used as the string argument to the VBA ASC function.
  • Case matters when using the Asc function.
  • The Asc function returns an integer number between 0 and 255.
  • The ASCII codes for A-Z and a-z in VBA are 65-90 and 97-122, respectively.
  • A macro’s or sub-procedure’s name cannot be the name of a VBA default function. The sub-procedure will display an error if labeled as such.

The Excel VBA ASC function is a powerful tool for working with ASCII character codes in VBA code. By providing the corresponding character code for a given character, the ASC function makes it easier to manipulate and compare text data in your Excel workbooks. Simply provide a string argument to the function to receive an integer value that represents the ASCII code for that character. Understanding how to use the ASC function can help you automate repetitive tasks and become a more effective VBA programmer.

For ready-to-use Dashboard Templates:

  1. Financial Dashboards
  2. Sales Dashboards
  3. HR Dashboards
  4. Data Visualization Charts

Leave a Comment

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

Categories