CHR() Function VBA

Posted: May 10, 2013 by Theodoulus in VBA
Tags: , ,

In VBA, we have CHR() function that returns a character based on the ASCII value.

Syntax:
The syntax for CHR() function is
CHR(Ascii_Value)
The parameter “Ascii_value” is used to retrieve the character.

How to use:

                                 Dim chrA   As String
                                 ChrA=CHR(87)

In the above example, chrA will hold a value “W” which is ASCII equivalent to 87.

Problem Statement:
To insert an IF() statement in a cell as formula, which includes double quotes.
Solution:
People fix this in two ways

Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0,"""",Sheet1!A1)"

Or

Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0," & CHR(34) & CHR(34) & ",Sheet1!A1)" 

In the first way, we may get confused with the number of double quotes to put in whereas in the second example there is no chance of confusion. So, the second one is the nice choice.

See you soon!

About these ads

Share your thoughts/feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s