VBA writing to file is rounding numeric values - how to prevent?

给你一囗甜甜゛ 提交于 2021-02-05 09:43:44

问题


In the below code I am having problems making sure the file writer does not round my number off to a certain number of decimal places. I need to use a variant because sometimes the value is string and at other times it is a number.

How can I force it to write exactly what the variable is? For example the below code might show 0.00038 and I want to show the exact value.

Dim MyFile1 As Variant
Dim MyNumber as Variant

MyFile0 = "C:/myfile.csv"
fnum0 = FreeFile()
Open MyFile0 For Output As fnum0

MyNumber = 0.0003759656

Print #fnum0, MyNumber

回答1:


Your code is fine. The issue is with excel. When opening a CSV in excel, including thru VBA, excel detirmines what a cell is. Typically, if it lloks like a number with more then 5 characters it will express it in Scientific notation or rounded to 5 places.

Note sure what you are doing with the CSV file before or where you are getting it from, but here are some options to prevent excel from changing your data:

In VBA use the Workbook.OpenText command to open the CSV with either:

-the all excel cells as text*

-the column stored number with so many decimal places (note a max of 30 decimal places apply)

-a particular column store as text.

--A full list of option syntax here http://msdn.microsoft.com/en-us/library/office/bb22351(v=office.12).aspx

You may also import your CSV into an excel spreadsheet, it will give you the option to choose data type for each column. Then run the VBA against the excel file.

If you are not doing formulations in excel, I would recomend storing the number as a text string. VBA automatically converts strings of numbers into numeric values when needed.




回答2:


It's likely that you're experiencing floating point errors. They are are very small errors that occur when numbers are converted from/to base 10 (human) to/from base 2 (computer). For your purposes, you need to determine what it means that two values are not equal. It's not just val1 <> val2 because that won't account for the tiny errors. If you're dealing with money, for instance, you probably don't care about anything less than a penny. So you might determine inequality as ABS(val1 - val2) > .001. You just need to determine what your tolerance for equality is and compare to that standard.




回答3:


It may be that your number is not being stored as a number in vba, and when you write it Excel converts it. Say you're number is 1.789 and you write it to a cell that is formatted as 'GENERAL'. Excel will write 1.79 (and think that's the number, not even a formatting issue).

What I've discovered, is if you convert the number to a decimal first CDEC(YOURNUMBER), it will write it correctly. For good measure, I also verify that the cell is '.NUMBERFORMAT = "GENERAL'



来源:https://stackoverflow.com/questions/16032048/vba-writing-to-file-is-rounding-numeric-values-how-to-prevent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!