Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Str Function in VBA for Excel adds a character to the string

I am converting an integer to string using the str() function

However, I noticed that the str() function would return an extra character to the string.

For example, MsgBox(Len(str(1))) would return 2.

What is the extra character being appended?

like image 501
Steveng Avatar asked Apr 04 '12 02:04

Steveng


People also ask

How do you add to a string in VBA?

To concatenate two strings using a VBA code, you need to use the ampersand. You can use an ampersand in between two strings to combine them and then assign that new value to a cell, variable, or message box. In the same way, you can concatenate more than two values as well.

What does STR do in VBA?

The Microsoft Excel STR function returns a string representation of a number. The STR function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel.

How do you check for a character in a string in Excel VBA?

InStr function finds the position of a specified substring within the string and returns the first position of its occurrence. For example, if you want to find the position of 'x' in 'Excel', using the Excel VBA InStr function would return 2.


1 Answers

From Excel 2010 help:

"When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied."

And sure enough this statement returns True in the debug window:

? left(str(1),1) = " "
like image 64
Doug Glancy Avatar answered Nov 14 '22 23:11

Doug Glancy