In Excel VBA, I want to format some strings with 2 digits and others with 3 (only knowing at run-time which I want to use). This can be done using the format function, e.g., Format(number, "0.00") or Format(number, "0.000"). But the function ignores the argument between quotation marks when sent as a string argument. Does anybody know what variable type to use here for the argument and provide an example? Thanks so much, I tried everthing I can think of (double quotation marks etc.), but this specific problem is not covered on any website.
Related
I have a formula reducing to
=SEARCH("Exchange~server","Exchange~server")
Excel errors on this with "A value used in this formula is of the wrong data type". I know my data can be fixed with
=SEARCH(SUBSTITUTE("Exchange~server","~","-"),SUBSTITUTE("Exchange~server","~","-"))
But, what does excel do to make the first option not work?
Edit: updating title to reflect true issue
Note that:
=FIND("Exchange~server","Exchange~server")
works returning a value of 1
However, when you use SEARCH, wild card characters are allowed in Find_text. The rules for that include one that states that a tilde-x eg (~s) will search for only the s. This is generally used to be able to return the literal wild-card characters * and ?. But it will be true for any character following the tilde.
So your find_text argument reduces to "Exchangeserver" which, of course, cannot be found if you are searching "Exchange~server" with the literal tilde.
To do that, you must change find_text to "Exchange~~server" (double tilde ~~). The ~~ will reduce to the literal ~.
i.e: =SEARCH("Exchange~~server","Exchange~server")
I am using this formula in excel
=CHOOSE(RANDBETWEEN(1,6),”Item1″,“Item2”,“Item3”,“Item4”,“Item5”,“Item6”)
It's supposed to pick one of those values at random and return it in the cell, but I just get #NAME?.
What is the issue?
The #NAME? error signifies that something needs to be corrected in the syntax, so when you see the error in your formula, think of the syntax.
I don't see anything wrong with the syntax. But if you have copy-pasted the code into your Excel from a blogpost or somwhere else, maybe the double quotes characters are not the chars that Excel expect.
Try the following code instead, which has the standard double quotes.
=CHOOSE(RANDBETWEEN(1,6),"Item1","Item2","Item3","Item4","Item5","Item6")
Further info here
Whay does this happen? That comes from how computers work with characters (It's called ASCII). Maybe it's a very deep topic for the scope of this post. But, basically most programming languages accept only one type of character, while the platform (your Operating System) usually allows you a variety of representations for the same symbol (but different ASCII code), and that are thought for different contexts. In short, the double quotes look similar but the ASCII code are different:
" this char is accepted by Excel for enclosing values (open and close)
“ this char is not accepted by Excel for enclosing values (open)
” this char is not accepted by Excel for enclosing values (close)
For technical reference on this you can see this question
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 have several variables of the form:
1 gdppercap
2 19786,97
3 20713,737
4 20793,163
5 23070,398
6 5639,175
I have copy-pasted the data into Stata, and it thinks they are strings. So far I have tried:
destring gdppercap, generate(gdppercap_n)
but get
gdppercap contains nonnumeric characters; no generate
And:
encode gdppercap, gen(gdppercap_n)
but get a variable numbered from 1 to 1055 regardless of the previous value.
Also I've tried:
gen gdppercap_n = real(gdppercap)
But get:
(1052 missing values generated)
Can you help me? As far as I can tell, Stata do not like the fact that the variable contains fraction numbers.
If I understand you correctly, the interpretation as string arises from one and possibly two facts:
The variable name may be echoed in the first observation. If so, that's text and it's inconsistent with a numeric variable. The root problem there is likely to be a copy-and-paste operation that copied too much. Stata typically gives you a choice when importing by copy-and-paste of whether the first row of what you copied is to be treated as variable names or as data, and you need the first choice, so that column headers become variable names, not data. It may be best to go back and do the copy-and-paste correctly. However, Stata can struggle with multiple header lines in a spreadsheet. Alternatively, use import excel, not a copy-and-paste. Alternatively, drop in 1 to remove the first observation, provided that it consistently is superfluous.
Commas indicate decimal places. destring can easily cope with this: see the help for its dpcomma option. Stata has no objection to fractions; that would be absurd. The problem is that you need to flag your use of commas.
Note that
destring is a wrapper for real(), so real() is not a way round this.
encode is for mapping genuine categorical variables to integers, as you discovered, and as its help does explain. It is not for fixing data input errors.
You can write a for loop to convert a comma to a period. I don't quite know your variables but imagine you have a variable gdppercap with information like 1234,343 and you want that to be 1234.343 before you do the destring.
For example:
forvalues x = 1(1)10 {
replace gdppercap = substr(gdppercap, 1, `x'-1) + "." + substr(gdppercap, `x'+1, .)
if substr(gdppercap, `x', 1) == ","
}
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)