VBA Type mismatch Error - inconsistency - vba

Okay, I'm so confused right now, I have written some VBA which is a plugin in Excel. There is a line which reads:
obsValue = CDbl(dataSplit(1))
The value of dataSplit(1) is the String 21440.5395043259
This line of code works okay...for me.
For another user of my code they get
Run time error '13:' Type mismatch.
They click on debug, it takes them to this line of code, and they hover their cursor over the dataSplit varaible, and it does indeed read the same value as above.
We are both using Excel 2010.
How can this be?

The language settings of Excel will determine what the decimal separator is. It seems likely that for your user, their language settings are different, and so the "." is probably treated as a thousands separator. The CDbl function is locale aware, so it "sees" numeric strings according to the regional settings of the current user.
You could try using the Val function, which will always treat the period as a decimal separator.
obsValue = Val(dataSplit(1))

Related

How to change VBA array decimal separator?

I would like to change VBA array decimal separator to dot. I see it as comma. I tried: Application.DecimalSeparator="."
But when I get the value as MyString = matrix(2, 1), the decimal separator in VBA arrays maliciously persists as comma. I am not able to get rid of the pest.
Is there a way to detect which system separator for VBA arrays is used?
VBA uses quite a few bits drawn from various parts of the platform to work out which decimal and thousands separator to use. Application.DecimalSeparator changes a few instances (mostly on the workbook); you can tweak others at the OS level, but even then though you get to a couple of cases where you can't change the settings.
Your best bet is to write a simple function to check which separator your platform uses based on a trial conversion of say 1.2 to a string and see what the second character ends up being. Crude but strangely beautiful.
Armed with that you can force an interchange of . and , as appropriate. Naturally then though you will have to manage all string to number parsing yourself, with some care.
Personally though I think this is epitomises an unnecessary fight with your system settings. Therefore I would leave everything as it is; grin and bear it in other words.
You have to change it in system settings, Excel takes this kind of settings from system.
I have end up with this function which does exactly what I want. Thank you all for answers, comments and hints.
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function

Named ranges won't work

Whenever I try to make a named range in Excel I keep getting an error. I believe my formula is correct:
=OFFSET($B$2,0,0,COUNTA($B$2:$B$200),1)
However when I press OK I keep getting the dialog screen which states Excel found a problem with my formula.
Then it highlights the following part of the formula: $B$2,0,0,COUNTA.
I looked through various tutorials where this formula should be correct.
Can someone help me out on this?
One way to see this error message is if you are using the incorrect argument separators. For example, many locale's use the semicolon ;.
Try replacing the commas in your formula with semicolons
=OFFSET($B$2;0;0;COUNTA($B$2:$B$200);1)
or whatever your locale argument separator is.
Thanks, it were indeed the seperators which caused the problem: I have to use ; instead of a , - this tricked me as all of the tutorials I watched used the comma as well.

Excel VBA Run-time error 1004 when inserting or value formula into cell

I got the run-time 1004 error when It try to insert a formula into a cell
Range("B64").Value = "=INDEX(AK7:AK123;G74;1)"
//I also tried
Range("B64").Formula = "=INDEX(AK7:AK123;G74;1)"
//And
Range("B64").FormulaR1C1 = "=INDEX(AK7:AK123;G74;1)"
But this gives the error. If I try inserting a number or a regular string like "test" it does work, but like this it doesn't. Im new to VBA and im wondering why this would give a problem since it would never in languages im used to.
Inserting a formula with VBA requires that you use EN-US standards like,
Range("B64").Formula = "=INDEX(AK7:AK123, G74, 1)"
... or use the regional formula attribute like,
Range("B64").FormulaLocal = "=INDEX(AK7:AK123; G74; 1)"
You may have to also change INDEX to the regional equivalent. The latter is necessary when you have a system with regional settings that do not use the EN-US standard of a comma for a list separator.
see Range.FormulaLocal Property (Excel) for more information.

Handling ⅝ (five eights) Vulgar fraction as part of a string in VBA

As part of an access application, i retrieve data from sql server. I have a string field that contains ⅝ in one of the rows, and when i attempt to insert that value into an MsAccess recordset, i get an error
Multiple-step operation generated errors. Check each status value. Here is my code
sFieldValue = getValue() ' when i add a watch, the '⅝' is replaced by a ? e.g. "The result is ⅝" will be shown as "The result is ?"
Rs(sFieldName) = sFieldValue ' error is thrown
I then attempted to hard code the value in VBA
sFieldValue ="⅝"
And the moment i type '⅝' it automatically changes to a question mark
sFieldValue ="?"
I would like to know how i can support characters such as "⅝". The other fractions work just fine, e.g. '½'. I do not want to do any calculations, the fractions are part of a string that comes from a SQL Server, and the problem is, i get a runtime error when i try to add a string value that contains a fraction to a recordset in access.
from this page, it shows that some fractions are supported in utf-8
The problem was that the field type of Rs(sFieldName) was adVarChar. So to fix the problem, i used a adVariant field type in my recordset, and it now accepts the ⅝ vulgar character.
I am creating the recordset on the fly.
Edit: adVarWChar is more appropriate, see comment below

Range(...).Formula does not translate fully

I cannot figure this one out.
We use mostly french-version Excel (as we live in a french-speaking province of Canada). Somewhere in VBA code I set a cell's formula directly. Normally, we have to write the formula in english and Excel does the translation (writing the formula in any other language than english in VBA results in an error as far as I know). However, only HALF of this equation is translated which I think is causing me issues (writing the correct formula in another cell yields different results and most probably right results).
range("J2").Formula = "=round(IF(F2="",0,F2),2)-round(IF(G2="",0,G2),2)"
Is translated to this in the cell:
=ARRONDI(SI(F2=",0,F2),2)-round(IF(G2=",0,G2),2)
As you can see, the right part should read "ARRONDI(SI(.." but it does not read that way. I have tried adding spaces, removing the minus sign altogether, etc. Nothing works, it's always half translated. Any idea ?
In VBA you neexd to escape your quotations like this:
range("J2").Formula = "=round(IF(F2="""",0,F2),2)-round(IF(G2="""",0,G2),2)"
This is because the " Character is used in VBA as the start / end of a string. So if you want ot include it IN a string you need to type it twice in a row.