Excel OleDb Data Types - excel

Does anyone have a list of data types I can use in a CREATE TABLE statement for Excel? I have searched and cannot come up with anything; at least not anything official. From one place, I found:
CHAR(255)
MEMO
INT
DATE
TIME
What about other numeric types? DECIMAL, etc? DATETIME (together)?
Thanks in advance.

Be careful: Excel natively supports only the the following data types (so any other data type will be converted to one of these)
Double precision floating point (this handles integers, longs, dates, times, currency etc) string Boolean Error
These data types can be formatted by the Excel renderer to look like currency, integer, date time etc.

Related

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

When does FlexCel's TCellValue.IsDateTime always return false?

I am using TMS' FlexCel Excel document component. I am - however - having a problem with dates. As I understand it, Excel has three types: Strings, numbers and datetimes. This is also represented in FlexCel's TCellValue's methods, such as IsString, IsNumber and IsDateTime.
However, IsDateTime always returns False. TCellValue has a ToDateTime function that will always return a TDateTime. If the original cell value is not a date, the time will be "1899-12-31T23:59:59".
I am however not keen on the idea using DateUtils's YearOf() on 1899 to detect whether it truly was a date. What if the cell contained a float (double)? That would become a proper TDateTime.
I have made sure that the format for these cells are a datetime. When exporting to Excel's XML Spreadsheet 2003 format, I can see that the cells are properly detected as datetimes by Excel. Why doesn't FlexCel detect the same thing?

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

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
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.
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.
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'

Excel VBA unexpected truncation

So I've spent the better part of the day debugging my VBA Class module and came upon a worrying phenomenon.
On the main spread sheet I have a cell formatted as "currency", with 9 decimal place precision. In my VBA class, I have an attribute (a double) which takes on that cell value upon instantiation. i.e.,
myClass.loanDefault_7 = Range("loanDefaults")(1,7)
However, when I check the value stored in myClass.loanDefault_7 after the assignment, it has only 4 decimal place precision.
My question is why is VBA automatically (without my permission) truncating down this value? I want to understand why this happens, and how I can prevent it in the future.
Any help is much appreciated!
Try using the Value2 property instead:
myClass.loadDefault_7 = Range("loadDefaults")(1,7).Value2
From MSDN:
The only difference between this property and the Value property is that the Value2 property doesn’t use the Currency and Date data types. You can return values formatted with these data types as floating-point numbers by using the Double data type.
It seems that the Value property will automatically coerce the value into a Currency data type (which, AFAIK, is only capable of representing 4 numbers to the right of the decimal point).

Reading mix between numeric and non-numeric data from excel into Matlab

I have a matrix where the first column contains dates and the first row contains maturities which are alpha/numeric (e.g. 16year).
The rest of the cells contain the rates for each day, which are double precision numbers.
Now I believe xlsread() can only handle numeric data so I think I will need something else or a combination of functions?
I would like to be able to read the table from excel into MATLAB as one array or perhaps a struct() so that I can keep all the data together.
The other problem is that some of the rates are given as '#N/A'. I want the cells where these values are stored to be kept but would like to change the value to blank=" ".
What is the best way to do this? Can it be done as part of the input process?
Well, from looking at matlab reference for xlsread you can use the format
[num,txt,raw] = xlsread(FILENAME)
and then you will have in num a matrix of your data, in txt the unreadable data, i.e. your text headers, and in raw you will have all of your data unprocessed. (including the text headers).
So I guess you could use the raw array, or a combination of the num and txt.
For your other problem, if your rates are 'pulled' from some other source, you can use
=IFERROR(RATE DATA,"")
and then there will be a blank instead of the error code #N\A.
Another solution (only for Windows) would be to use xlsread() format which allows running a function on your imported data,
[num,txt,raw,custom] = xlsread(filename,sheet,xlRange,'',functionHandler)
and let the function replace the NaN values with blank spots. (and you will have your output in the custom array)

Resources