Variable using decimal comma instead of decimal point - excel

I have been using Excel VBA for a while now and I have come across a problem that I have never encountered before. I am using someone elses computer that is set up as Polish, so the decimal separator is set to comma. But even when I had my computer set up similarly in the past I did not have any problem.
This current project creates a drawing in Visio.
So,
I have a variable of type double that is calculated as pgeWidth = 550 / 1.4
What I would expect is that VBA would calculate pageWidth = 392.857...
However what VBA is doing is pgeWidth = 392,857... If I put a break in and check the value of pgeWidth it shows the value with a comma separator. pgeWidth is then set to the page width property in Visio which thinks it should be 392857.... mm wide which obviously gives an error.
Why is VBA using a comma decimal separator instead of a point?

If you want to pass the value further with point, consider converting variable to a string and then replacing comma with point:
pgeWidth = replace(CStr(pgeWidth), ",",".")

Related

How to remove quotes from '156.70' in Python?

I'm trying to make a rounding function in Python to make proper significant figures but I don't know how to remove the quotes from the answer, an ex. is '156.70', I want to have the number with 2 decimal place precision, and
round(156.70,2) returns 156.7.
I have tried this: format(round(156.70,2),'.2f'), but it places the answer in single quotes like I stated above. Is there a way to round directly with the floating point zero, or is there a way to remove the quotes?
I have also tried the strip command which does not remove the quotes.
I also tried converting the answer (156.70) to a numpy array to remove the quotes the following way:
ares1 = np.array(format(round(ans,2),".2f"))
values = ares1.item().split(' ')
ares = np.asarray(values, dtype='float')
Where 'ans' is the value 156.7 I want to round to two precision points. Thanks for any help.
Precision doesn't work in programming as it works in real life. By default, numbers have infinite precision, and any decimal places after the defined decimal places will be zeroes. So, your number 156.7 is the same as 156.70 and 156.7000, as they will all be stored identically in memory.
Now with that out of the way, you can definitely "format" the number when outputting it. That will not edit the actual stored value, but will give you a String representation of the number for display purposes only. For that, I'll refer you to this StackOverflow question.

How to use =LEFT and =LEN function with scientific notation in Excel VBA?

I'm trying to write an Excel macro using VBA that will return only the first 5 numbers in a cell when the length of that cell exceeds 20. The field normally returns 15-digit alphanumeric results (which I need to leave alone) but in certain exceptions will return a 5-digit number with a multitude of zeroes following it (1234500000000000000000000...) which Excel converts into scientific notation (1.2345E+160). I am able to convert the cells to numbers instead of scientific notation and view the whole number.
I've tried to use code such as =IF(LEN(A1)>20,LEFT(A1,5),A1) and it just returns 1.2345E+160. Even though the whole number is displaying, Excel still thinks the cell length is 11 and won't display the first 5 digits.
I've also tried lines such as =IF(A1="E",LEFT(A1,6),A1) thinking it would detect the E, return 1.2345, and I could just remove the decimal points, but that didn't work either (it just returns the original 1.2345E+160).
I got the same results whether the cell was formatted as number or text. Is there a way around this?
Thank you for your time!
You are trying to use string manipulation on a number. Instead use math:
=A1/1E+160
If you do actually want to treat this thing as text, understand that the underlying value being stored is your 12345000000000000... and there is no decimal point in that thing. So you'll have to convert to text and add the decimal:
=LEFT(TEXT(A1,"0"), 1) & "." & MID(TEXT(A1,"0"), 2, 4)
But that's pretty ugly. I would just stick with math.

Decimal number in string-data-type with large amount of decimals always interpreted as large integer (regional decimal separator issue)

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

How can I make sure Excel does not change number format of a data label in other language versions?

I have a workbook with VBA code that specifies a number format for data labels in a horizontal bar chart.
Chart.SeriesCollection(1).DataLabels.NumberFormat = "0.0"
In my copy of Excel (O365) in English, output is as expected, e.g. 3.7. When the workbook is opened in another copy of Excel (O365) in a language other than English, Excel appears to insert a backwards slash that can be seen in the Format Code -field of the Format Data Labels -sidebar (i.e. 0\.0). Where output should be e.g. 3.7, output becomes 0.4.
The issue persists when I load up the workbook after it has been touched by the non-English Excel - my version doesn't throw away the backward slash. The issue goes away when I delete the backwards slash, and output is as expected again.
It seems to me Excel treats the leading zero and decimal point as plain text and the second zero as the intended numerical character.
How can I make sure Excel maintains the number format specified in VBA when a workbook is opened in a different language version?
Don't have a direct solution, but if this persists with NumberFormat, you can programmatically use combinations of built-in Excel functions to achieve the same numbers.
for ex:
number = Round(number, 1)
In this case, you can leave the NumberFormat as "General" or something similar
It appears the easiest solution is to programmatically disable decimal separators and replace them with your own, as described here.
Application.DecimalSeparator = "."
Application.ThousandsSeparator = ","
Application.UseSystemSeparators = False
I came across this solution in this thread while searching for how to determine the language version of Excel.
Another solution is to use the decimal separator prescribed by the system when defining the number format. This solution seems to also produce the desired result (one decimal place displayed, even if 0), though with whatever decimal separator the system has defined:
Chart.SeriesCollection(1).DataLabels.NumberFormat = "0" & Application.International(xlDecimalSeparator) & "0"

Excel – locale-independent Number Format

I looking for a number format that is locale-independent.
Let's say that I want that my Excel file use an a character as a thousand separator and a b character as a decimal separator on every computer (so this question is not about local Excel settings). The second wish is that the number has always two decimal places.
Can I do this only with a number format without VB code?
After some attempts, I came to this format: ###\a###\a##0\b.00.
1234567.89 > 1a234a567b.89
The problems are:
I must repeat ###\a section and I can not get rid of dot decimal separator (the decimal places are not taken into account without the dot character).
Can I do this only with a number format without VB code?
No.
Even if you can get your separator characters inserted, you will not be able to remove the decimal separator.
The decimal separator is displayed in the cell whenever it is included in custom number display format settings.

Resources