How to Use VBA Asc Function (5 Practical Examples)

Download Practice Workbook


Introduction to VBA Asc Function

The VBA Asc function is a built-in function in Excel. This is a String/Text Function. We can use it as a VBA function (VBA) in Excel. This function can be used as a VBA function in Excel macros. We can also enter this code using the Microsoft Visual Basic Editor. The Microsoft Excel VBA Asc function returns the ASCII value of a character or the first character in a string passed as arguments.

Syntax & Arguments:

=Asc(string)

ARGUMENT REQUIRED OR OPTIONAL VALUE
string Required If string consists of more than one character, the ASC(VBA) function returns the ASCII value of the first character in the string

Types:

This function can’t be used as a worksheet function. You can only use it in VBA Subprocedures.

To find the ASCII value of a character or a text in an Excel worksheet, you can use the CODE function.


5 Examples to Use Excel VBA Asc Function

Example 1 – Apply a Word Using VBA Asc Function

Steps:

  • Go to Developer.
  • Select Macros.

Example to use VBA Asc Function

  • A new window will open.

Example to use VBA Asc Function

  • Enter a macro name.
  • Select the Macros in the option to This Workbook.
  • Click on the Create button.

Example to use VBA Asc Function

  • You will get the following window.

Example to use VBA Asc Function

  • Enter the code given below:
Sub Code()
Dim Result1
Result1 = Asc("Robin")
MsgBox Result1
End Sub

Example to use VBA Asc Function

  • Run the code by pressing f5.

The pop-up window will show the result.

Example to use VBA Asc Function

  • Run the code for both the name Kane and Aaron by changing the Name portion of the code.

Example to use VBA Asc Function


Example 2 – VBA Asc Function to Use an Uppercase Letter

Steps:

  • Enter the code given below in Microsoft Visual Basic.
Sub Code() 
Dim Result1 
Result1 = Asc("A") 
MsgBox Result1 
End Sub

  • Press f5 to run the code.
  • A pop-up window will open.

Example to use VBA Asc Function


Example 3 – Insert a Lowercase Letter Using VBA Asc Function

Steps:

  • Enter the code given below in the Microsoft Visual basic window.
Sub Code()
Dim Result1
Result1 = Asc("a")
MsgBox Result1
End Sub

  • Press f5 to run the code.
  • A pop-up window will display the result.


Example 4 – Introduce a Symbol with VBA Asc Function

Steps:

  • Enter the code given below in theMicrosoft Visual Basic window.
Sub Code()
Dim Result1
Result1 = Asc("@")
MsgBox Result1
End Sub

  • Press f5 to run the code.
  • A pop-up window will display the result.


Example 5 – Apply VBA Asc Function to Insert a Blank Space

Steps:

  • Enter the code given below in Microsoft Visual Basic Window.
Sub Code()
Dim Result1
Result1 = Asc(" ")
MsgBox Result1
End Sub

  • Press f5 to run the code.
  • A window will display the result “Invalid procedure call or argument” because there is no argument in the function.


Further Readings


Things to Remember

  • Asc(VBA) function is a VBA function. So, it can’t be used as a worksheet function. You can only use it in VBA Subprocedures.
  • This function returns value only for the first letter of a string. This is why values for Aaron & A are the same.
  • This function is case-sensitive.
Get FREE Advanced Excel Exercises with Solutions!
Kawser Ahmed
Kawser Ahmed

Kawser Ahmed is a Microsoft Excel Expert, Udemy Course Instructor, Data Analyst, Finance professional, and Chief Editor of ExcelDemy. He is the founder and CEO of SOFTEKO (a software and content marketing company). He has a B.Sc in Electrical and Electronics Engineering. As a Udemy instructor, he offers 8 acclaimed Excel courses, one selected for Udemy Business. A devoted MS Excel enthusiast, Kawser has contributed over 200 articles and reviewed thousands more. His expertise extends to Data Analysis,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo