Excel CODE & CHAR Functions, VBA Asc & Chr Functions

Excel Text and String Functions:

Excel CODE & CHAR Functions, VBA Asc & Chr Functions, with examples.

Related Links:

1. Using VBA Chr and Asc functions to convert excel column number to corresponding column letter, and column letter to column number.

2. ASCII Code, Extended ASCII characters (8-bit system) and ANSI Code.

3. Excel Text and String Functions: TRIM & CLEAN.

4. Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE.


Excel CODE Function (Worksheet)

The CODE Function returns the identifying numeric code of the first character in a text string. A ‘character set’ maps characters to their identifying code values, and may vary across operating environments viz. Windows, Macintosh, etc. The Windows operating environment uses the ANSI character set, whereas Macintosh uses the Macintosh character set. The returned numeric code corresponds to this character set – for a Windows computer, Excel uses the standard ANSI character set to return the numeric code. Syntax: CODE(text_string). The text_string argument is the text string whose first character’s numeric code is returned.

The equivalent of excel CODE function in vba is the Asc function. Note that the Excel CODE function is the inverse of the Excel CHAR function. The formula =CODE(CHAR(65)) returns 65, because =CHAR(65) returns the character “A” and =CODE(“A”) returns 65.

In a Windows computer, Code function will return values as follows:

=CODE(“A”) returns 65.

=CODE(“B”) returns 66.

=CODE(“a”) returns 97.

=CODE(“b”) returns 98.

=CODE(“0”) returns 48.

=CODE(“1”) returns 49.

=CODE(“-1”) returns 45, which is the code for “-” (hyphen) .

=CODE(” “) , note the space between inverted commas, returns 32.

=CODE(“”), no space between inverted commas, returns the #VALUE! error value.

=CODE(“America”) returns 65, which is the code for “A”, the first character in the text string.

=CODE(A1) returns 66, using cell reference. If cell A1 contains the text “Bond”, CODE(A1) returns 66, which is the code for “B”, the first character in the text string.

=CODE(“?”) returns 63.

=CODE(“™”) returns 153.


Excel CHAR Function (Worksheet)

Use the CHAR Function to return the character identified to the specified number. A ‘character set’ maps characters to their identifying code values, and may vary across operating environments viz. Windows, Macintosh, etc. The Windows operating environment uses the ANSI character set, whereas Macintosh uses the Macintosh character set. The returned character corresponds to this character set – for a Windows computer, Excel uses the standard ANSI character set. Syntax: CHAR(number). The number argument is a number between 1 and 255 which identifies the returned character. The function returns the #VALUE! error value if this argument is not a number between 1 and 255.

The equivalent of excel CHAR function in vba is the Chr function. Note that the Excel CHAR function is the inverse of the Excel CODE function. The formula =CHAR(CODE(“A”)) returns A, because =CODE(“A”) returns the number 65 and =CHAR(65) returns A.

In a Windows computer, Char function will return values as follows:

=CHAR(65) returns the character “A”.

=CHAR(37) returns the character “%”.

=CHAR(260) returns the #VALUE! error value.

=CHAR(163) returns the character “£”.

=CHAR(A1) returns the character “B”, using cell reference. If cell A1 contains the number 66, CHAR(A1) returns the character “B”.


Examples of using the Excel CODE & CHAR Functions

Changing case (uppercase / lowercase) of alphabets in a string

Formula  =CHAR(CODE(A1)+32) returns the lowercase letter “b”, if cell A1 contains the uppercase letter “B”. This is because in the ANSI character set, the lowercase alphabets appear after uppercase alphabets, in an alphabetic order,  with a difference of exactly 32 numbers. Similarly, =CHAR(CODE(A1)-32) returns the uppercase letter “B”, if cell A1 contains the lowercase letter “b”.

Convert first character of the first word in a text string (consisting of letters), to lowercase, and first character of all subsequent words to uppercase. To convert the string “we like james bond” in cell A1 to “we Like James Bond” use the following formula:

=REPLACE(PROPER(A1), 1,1, CHAR(CODE( LEFT(PROPER(A1)))+32))

=PROPER(A1)   returns the text string “We Like James Bond”.

=CHAR(CODE(LEFT( PROPER(A1)))+32)   returns the lowercase text “w”.

The formula replaces the first character of the text, by first capitalizing it and then converting it to lowercase using CHAR/CODE functions.

It may however be noted, that the excel UPPER (or LOWER) function can also be used as alternate to CHAR/CODE functions. In this case the Formula  =REPLACE(PROPER(A1),1,1,LOWER(LEFT(PROPER(A1)))) will also give the same result and return the string “we Like James Bond”.

—————————————————————————————————————————-

Making the above formula more complex:

Convert first character of the second word in a text string (consisting of letters) to lowercase, and the first character of all other words to uppercase (wherein words are separated by a single space). To convert the string “we Like James bond” in cell A1 to “We like James Bond” use the following formula:

=REPLACE(PROPER(A1), FIND(” “, A1)+1, 1, CHAR(CODE(MID(PROPER(A1), FIND(” “, A1)+1, 1))+32))

=PROPER(A1)   returns text string “We Like James Bond”.

=FIND(” “, A1)+1   returns the position 4.

=MID(PROPER(A1), FIND(” “, A1)+1,1)    returns L.

=CODE(MID(PROPER(A1), FIND(” “, A1)+1,1))    returns 76.

=CHAR(CODE(MID(PROPER(A1), FIND(” “, A1)+1,1))+32)   returns the text “l”.

————————————————————————————————————————–

If a text string has irregular spacing (ie. having lead spaces and multiple spaces between words), insert the TRIM function which removes all spaces from text except for single spaces between words:

=REPLACE(PROPER( TRIM(A1)), FIND(” “, TRIM(A1))+1, 1, CHAR(CODE(MID( PROPER( TRIM(A1)), FIND(” “, TRIM(A1))+1, 1))+32)) 

Note: In addition to the CHAR, CODE, LEFT & MID functions, we have also used the excel worksheet PROPER function. Syntax: PROPER(text).  This function: (i) Capitalizes any letter in a text string which follows a non-letter character, and also the first letter of the text string; (ii) Converts to Lowercase all other letters.

————————————————————————————————————————–

For many letters and controls, it might be much easier to format or otherwise manipulate data with their ANSI codes than using your keyboard.

Image 1

If the address is currently split in different cells, and you wish to consolidate in a single cell in multiple lines, use the excel CHAR function as follows (refer Image 1):

=A1 & CHAR(10) & B1 & CHAR(10) & C1 & CHAR(10) & D1

The address is currently split in cells A1 to D1 and the above formula consolidates and formats into a single cell in multiple lines.

10 is the ANSI code (number) for line feed, and it provides the line breaks to format data.

Ensure that the cell text wrapping is enabled, by clicking on “Wrap Text”.

————————————————————————————————————————-

Image 2

Format a single line address into multiple lines, using the excel CHAR & SUBSTITUTE functions as follows (refer Image 2):

=SUBSTITUTE(A1,”, “,CHAR(10))

The address is currently mentioned in cell A1 in a single line, and the above formula formats it (in cell A3) by removing commas and into multiple lines.

10 is the ANSI code (number) for line feed, and it provides the line breaks to format data.

Ensure that the cell text wrapping is enabled, by clicking on “Wrap Text”.

————————————————————————————————————————–

Image 3

NOTE: To format an address with line breaks (appearing in multiple lines in cell A1) to a single line format (cell A3), use the formula (refer Image 3): =SUBSTITUTE(A1, CHAR(10), “, “)

——————————————————————————————————————————

To insert special characters or symbols in a text string, use the excel CHAR function:

=”Exchange Rates Graph of Pound (” & CHAR(163) & “), Euro (” & CHAR(128) & “) and Dollar (” & (CHAR(36) & “)”)

The above formula inserts currency symbols, using the CHAR function.

Returns the text string: “Exchange Rates Graph of Pound (£), Euro(€) and Dollar ($)”


VBA Asc function

The vba Asc function returns the corresponding character code (Integer data type) for the first letter of a string. Syntax: Asc(string). It is necessary to specify the string argument – it is a string expression whose first letter’s character code is returned. You will get a run time error for a string with no characters. For non-DBCS systems the normal range for the returned number is 0 to 255, but on DBCS systems the range is -32768 to 32767.

Use the AscW function to return a Unicode character code, but if the platform does not support Unicode then AscW will give results identical to the Asc function. AscW should not be used in the Macintosh environment because Unicode strings are not supported in Visual Basic for the Macintosh.

VBA Chr function

The vba Chr function returns a character (string data type) identified to the specified character code. Syntax: Chr(charcode). It is necessary to specify the charcode argument – it is the character code (Long data type) which identifies the returned character. The normal range for charcode is 0 to 255, but on DBCS systems (character set that uses 1 or 2 bytes to represent a character, allowing more than 256 characters to be represented – used for languages such as Japanese and Chinese), the actual range for charcode is -32768 to 65535. Numbers from 0 to 31 are the standard ASCII Non-Printing Control Codes.

Use the ChrW function to return a Unicode character, but if the platform does not support Unicode ChrW will give results identical to the Chr function. ChrW should not be used in the Macintosh environment because Unicode strings are not supported in Visual Basic for the Macintosh.

MsgBox Chr(65), returns the letter “A”.

MsgBox Asc(“A”), returns the number 65.

—————————————————————————————————————————-

Sub Chr_Replace()
‘using vba Chr() function to replace characters in a string

Dim str As String

‘replaces letter “A” with “B”

str = Replace(“ABC”, Chr(65), Chr(66))

‘returns the text “BBC”

MsgBox str

End Sub

————————————————————————————————————————-

Image 4

Sub TextFormat1()
‘using vba chr() function to remove commas and provide line breaks.

‘Chr(10) provides line feed/new line, and it replaces all instances of comma & following space, in the text string. Refer Image 4.
ActiveSheet.Cells(3, 1) = Replace(ActiveSheet.Cells(1, 1), “, “, Chr(10))

End Sub

———————————————————————————————————————————————————–

Sub TextFormat2()
‘inserting symbols using vba chr() function

Dim str As String

‘evaluates to text string “Exchange Rates Graph of Pound (£), Euro (€) and Dollar ($)”
str = Exchange Rates Graph of Pound (” & Chr(163) & “), Euro (” & Chr(128) & “) and Dollar (” & (Chr(36) & “)“)

‘displays the text string “Exchange Rates Graph of Pound (£), Euro (€) and Dollar ($)”
MsgBox str

End Sub

———————————————————————————————————————————————————–

Sub DeleteSpaces()
‘remove spaces from a text string, in vba, using the vba Replace function

Dim str As String

str = Removing  Spaces In Text String

‘removes space character with ANSI code 32

str = Replace(str, Chr(32), “”)

‘returns the text “RemovingSpacesInTextString”

MsgBox str

End Sub

———————————————————————————————————————————————————–

Sub RemoveNumbers()
‘remove characters, say all numericals, from a text string, using the vba Replace function

Dim str As String, i As Integer

str = RemovingNumbers 12in4 Text6 9String5″

‘chr(48) to chr(57) represent numericals 0 to 9 in ANSI/ASCII character codes

For i = 48 To 57

‘remove all numericals from the text string using vba Replace function:

str = Replace(str, Chr(i), “”)

Next i

‘returns text “Removing Numbers in Text String”

MsgBox str

End Sub

 

———————————————————————————————————————————————————–

 

Sub ManipulateTextString()
‘manipulate text string consisting of only letters & spaces: remove all spaces, separate each word with comma, capitalize first letter of each word & convert all other letters to lowercase

 

Dim str As String

 

str = red   Blue white  GREEN  yellow    ”

 

‘use the worksheet Trim function to remove all spaces from text leaving only single spaces between words – also deletes all leading & trailing spaces

str = Application.Trim(str)

 

‘replaces space character (ANSI code 32) with comma character (ANSI code 44)

str = Replace(str, Chr(32), Chr(44))

 

‘use the worksheet Proper function to: Capitalize the first letter & any other letter which follows a non-letter character; also converts to Lowercase all other letters.

str = Application.Proper(str)

 

‘returns the text “Red,Blue,White,Green,Yellow”

MsgBox str


End Sub

 

———————————————————————————————————————————————————–

 

 

VBA Codes:

Using VBA Chr and Asc functions to convert excel column number to corresponding column letter, and column letter to column number.

Leave a Reply

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

Scroll to top