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.

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

How to use:

                                 Dim chrA   As String

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.
People fix this in two ways

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


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!


Share your thoughts/feedback

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s