The documentation for char(number) states:
number: A number between 1 and 255...
=Char(1) and =Char(255) works, is there any work around to get Char(0) or is it impossible due to zero terminated string rules?
I strongly suspect that Excel uses zero-terminated strings internally.
A string returned by a VBA function which includes a null is truncated at the null when used in an Excel formula.
Related
In an excel program, I was debugging when I noticed that the string function of a number returned the number but with a space in front of it: str(1) returned " 1".
As far as I am aware, there is nothing much on the internet about this, so I would appreciate knowing whether it is a glitch, or if I am doing something wrong. There is a way of working around, if this may help:
Mid(Str(1), 2, 1) So what this does is take the second character of " 1", and if the number is 5 characters long, for example, change the number at the end to 5 eg Mid(Str(11111), 2, 5).
Thanks for any feedback, however, with the work-around, it is not 100% necessary that this question is answered.
If it helps, I use office 365...
From the VBA Reference:
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. Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number.
Please see:
Str Function
The leading space provided by VBA's Str function to positive numbers is for alignment purposes.
Text/strings are by default left-aligned on the worksheet. Since negative numbers receive a hyphen, positive numbers receive a space to maintain alignment.
Btw, if you omit VBA's Mid function's third argument (number of characters), all remaining characters from the second argument's starting position are returned. This means that Mid(Str(<any number>), 2) will always return the full number although any negative number's sign would be omitted. This does not work with the worksheet's MID function.
Background: I'm receiving data for my Excel application from an API in JSON format. For this matter I'm receiving numerical values as a string, as everything sent in JSON naturally is a text format - and so does VBA also interpret it. As I'm located in Denmark, using a different decimal separator than the native on in Excel (my Danish version utilizes , as separator rather than .).
Case:
This is causing quite a bit of trouble as Excel interprets this as a thousand-separator when converting the string to a number.
Searching for answers I've found that the best solution, normally, is to convert the string to double when using VBA, utilizing CDbl(string to convert to number).
This usually is the case, but in my case I'm receiving a number with a lot of decimals such as: "9.300000190734863".
When doing a CDbl("9.300000190734863") this results in a very large integer: 9,30000019073486E+15
Also, I don't think utilizing a replace() approach is feasible in my case as I might also have data that uses both decimal- and thousand separators at the same time, making my results prone to replacement errors.
However, inserting the string value directly into a cell within Excel converts the number correctly to 9,30000019073486 in my case.
Question: Can it be right that there's no way to mimic, or tap into, this functionality that Excel obviously is using when inserting the string into a cell?
I've searched for quite some time now, and I haven't found any solution other than the obvious: inserting the value into a cell. The problem here is that it's giving me some performance overhead which I would rather avoid.
You can swap the positions of the periods and commas in your input prior to casting as a double, in three steps:
Replace commas with 'X' (or some other value that won't appear in your data)
Replace periods with commas
Replace 'X' with periods
I regularly copy and paste values into an Excel spreadsheet and use the VALUE() function to convert them from text to numbers. However the following value is not getting converted and results in a #VALUE error:
−£13.24
I could do some complex string manipulation to remove the currency symbol, but just wondered if there was a simpler solution.
Any suggestions appreciated.
EDIT: I have just realised that it is not the currency symbol that is causing the problem, but the minus sign. I am copying the data from a website, and I guess it is using a different character encoding. Are there functions in Excel for handling character encoding?
I would use SUBSTITUTE to get rid of the currency character, e.g.
= SUBSTITUTE(D43,"£","")
And you could wrap the whole thing around a VALUE function, e.g.
= VALUE(SUBSTITUTE(D43,"£",""))
If you have issues with "long dash" vs. "short dash", (− vs. -), you can do this:
= VALUE(SUBSTITUTE(SUBSTITUTE(D43,"£",""),"−","-"))
Short dashes are required for Excel to recognize the string as a number.
=IF(LEFT(D43,2)="−£",-VALUE(MID(D43,3,LEN(D43)-2)),VALUE(D43))
This seems to do the job, but would be interested if there is a better/simpler solution.
I'm building a VBA program on Excel 2007 inputing long string of numbers (UPC). Now, the program usually works fine, but sometimes the number string seems to be converted to scientific notation and I want to avoid this, since I then VLook them up.
So, I'd like to treat a textbox input as an exact string. No scientific notation, no number interpretation.
On a related side, this one really gets weird. I have two exact UPC : both yield the same value (as far as I or any text editor can tell), yet one of the value gives a successful Vlookup, the other does not.
Anybody has suggestions on this one? Thanks for your time.
Long strings that look like numbers can be a pain in Excel. If you're not doing any math on the "number", it should really be treated as text. As you've discovered, when you want to force Excel to treat something as a string, precede it with an apostrophe.
There are a couple of common problems with VLOOKUP. The one you found, extra whitespace, can be avoided by using a formula such as
=VLOOKUP(TRIM(A1),B1:C:100,2,FALSE)
The TRIM function will remove those extraneous spaces. The other common problem with VLOOKUP is that one argument is a string and the other is a number. I run into this one a lot with imported data. You can use the TEXT function to do the VLOOKUP without having to change the raw data
=VLOOKUP(TEXT(A1,"00000"),B1:C100,2,FALSE)
will convert A1 to a five digit string before it tries to look it up in column B. And, of course, if your data is a real mess, you may need
=VLOOKUP(TEXT(TRIM(A1),"00000"),B1:C100,2,FALSE)
How do I get the last character of a string using an Excel function?
No need to apologize for asking a question! Try using the RIGHT function. It returns the last n characters of a string.
=RIGHT(A1, 1)
=RIGHT(A1)
is quite sufficient (where the string is contained in A1).
Similar in nature to LEFT, Excel's RIGHT function extracts a substring from a string starting from the right-most character:
SYNTAX
RIGHT( text, [number_of_characters] )
Parameters or Arguments
text
The string that you wish to extract from.
number_of_characters
Optional. It indicates the number of characters that you wish to extract starting from the right-most character. If this parameter is omitted, only 1 character is returned.
Applies To
Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Since number_of_characters is optional and defaults to 1 it is not required in this case.
However, there have been many issues with trailing spaces and if this is a risk for the last visible character (in general):
=RIGHT(TRIM(A1))
might be preferred.
Looks like the answer above was a little incomplete try the following:-
=RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))
Obviously, this is for cell A2...
What this does is uses a combination of Right and Len - Len is the length of a string and in this case, we want to remove all but one from that... clearly, if you wanted the last two characters you'd change the -1 to -2 etc etc etc.
After the length has been determined and the portion of that which is required - then the Right command will display the information you need.
This works well combined with an IF statement - I use this to find out if the last character of a string of text is a specific character and remove it if it is. See, the example below for stripping out commas from the end of a text string...
=IF(RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))=",",LEFT(A2,(LEN(A2)-1)),A2)
Just another way to do this:
=MID(A1, LEN(A1), 1)