Declare either .Text, .Value, .Value2 using array to fetch data from excel - vb.net

I use Excel automation with Excel interop. My code takes excel sheet using array fetch. As you can see below i take all of them as .Value2, however i would like to specify for example that one of my excel column to be taken as .Text. How to achieve that?
'Convert from interop object to native vb.net object, indexed 1 to length
Dim data As Object(,) = DirectCast(_xlWorkSheet.UsedRange.Value2, Object(,))
For row As Integer = 2 To data.GetUpperBound(0) - 1
Dim newDataRow As DataRow = dt.NewRow()
Dim dattime As DateTime = DateTime.FromOADate(data(row, 11))
Next

i have one column in excel that has format: [h]:mm:ss means hours could exceed clock hour so means there could be e.g 783:34:12. When i tried to use formatting for instance: NumberFormat ="#" or whatever else i always got wrong result.
Based on that description, I will assume that the value is entered into Excel as a numeric Double. i.e.: 783:34:13 equals 32.64875.
In VB.Net, you could generate a TimeSpan structure to yield the resulting day, hour, minutes and seconds components. In the following, rng is an Excel.Range representing a single cell.
Dim val As Double = CDbl(rng.Value2)
Dim ts As TimeSpan = DateTime.FromOADate(val) - DateTime.FromOADate(0)
Now if you wanted to format this TimeSpan as a string similar to that displayed in Excel, you could do something like this:
Dim s As String = String.Format("{0}:{1:00}:{2:00}", (ts.Days * 24) + ts.Hours, ts.Minutes, ts.Seconds)
The reason I am recommending this technique over getting the Text property of an Excel.Range is that the Text property will return exactly what you would see in Excel including the ever helpful "###" when the column is not wide enough to display the formatted value.
Edit To Address Comments:
can you explain why you do this: - DateTime.FromOADate(0) ?
Excel encodes a date-time value as a decimal number of 24 hour periods (days) from a specified date-time that has the value of zero. Your data is apparently making use of this fact to allow you to have a cell with a value of 783:34:12 (783 hours, 34 minutes, 12 seconds) or 32.64875 as a decimal value.
In order to retrieve the offset in (days, hours, minutes, seconds) the original value represents, you need to subtract date-time represented by its basis value (zero).
i saw mismatch e.g: in excel : 0:04:07 (real value behind is: 12:04:07 AM) after your function i get this: 0:04:06 (so 1 sec diffrence why is that?
I can not reproduce this issue. It is likely a rounding issue due too the limitations of floating point value representation.
Also, be advised that Excel supports two different date basis systems; the 1900 Date System (the default) and the 1904 Date System. The DateTime.FromOADate function does its conversion based on the 1900 Date System. The difference between these two systems is the date that is treated as zero. You should check the WorkBook.Date1904 property to see if you need to add addition days (1462 days) to the value retrieved from Excel when converting to a .Net DateTime.
This could effect the result of this code:
Dim dattime As DateTime = DateTime.FromOADate(data(row, 11))
See: Differences between the 1900 and the 1904 date system in Excel for more information.

Related

VBA date format code

I was wondering if there is any possibility to write a VBA code where the column A should always have a date format like this: 12.10.2017 (not 12/10/2017 or 12-10-2017). If anything else is written in the column A like "12" or "car" the entry should be deleted. It has to accept only the date format mentioned above.
I used data validation for this, with length 10 and the date format to take only "." into consideration, but I want to do it as a VBA code instead.
Thanks!
A valid date is a long representing the number of days since the 1st january 1900. So a valid date would be 45603. You can display this date in any format you wish using the format codes d, m and y . So to display the date as dd.mm.yyyy then set that numberformat in the cells in column A. Your problem though is that Excel will only accept a date entered as either a long or in a built in date format (using /, - or space as a separator). You could allow the users to enter a text string in the format dd.mm.yyyy and then convert that string into a date and then reject it if the conversion didn't result in a valid date - but wouldn't it be easier to just train your users to enter dates correctly?

Values vary after conversion from excel

I am trying to get values from one of the column from excel and i am facing strange issue that i cannot overcome so far.
Excel cells text we working on
Column format is set to: [h]:mm:ss so means hours could exceed 12/24.
When i am getting that values they are in double format as excel probbaly stores it in that way therefore i decided to write function to convert it back again to hours, minutes and seconds so i did that function:
Public Shared Function parseExcelHour(cellInput As String) As String
Dim excelHour As Double = 0
Dim hour As Integer
Dim min As Integer
Dim sec As Integer
Try
excelHour = [Double].Parse(cellInput)
Catch
End Try
sec = CInt((excelHour * 1440 * 60) Mod 60)
min = CInt((excelHour * 1440) Mod 60)
'mod (%) takes only the remainder as an int (if 5/4 = 1.25 , % only takes the number 1 that cannot be divided into an integer)
hour = CInt((excelHour * 1440) / 60)
' with the int cast you get only an integer.
Return hour & ":" & min & ":" & sec
End Function
However when i see the results, they are vary between excel and what i get after conversion. For three of them hours are either -1 or +1 if you compare. Also in one case we have additional + 1 minute. I suppose there is wrong hour calculation but i could be in wrong. See on screeshoot:
Results
Does anyone knows why i got those differences? Is that because i am missing something within my method or something else.
Excel stores a full Datetime equivalent as one double. The part before the decimal point is the days (since 1.1.1900; 1.1.1904 on Mac; note the bug that 1900 is faultily cosnider a leap year in Excel).
The part after is the time of the day, wich is what you apparently want:
https://stackoverflow.com/a/981865/3346583
What you are seeing in excel is meerely a ToString Foramting of the double value. Same way DateTime.ToString() would give you a string representation of whatever value is actually stored (most often a realy big unsigned int, with the ticks since a date).
A difference in full hours sounds like it might be a Timezone issue. But I am not aware that Excel stores Timezones in the first place (or what default timezone it asumes).

Changing date formats in Excel using

I have a excel sheet in which a column has date date in the format "yyyyMMdd" and I want to format it as "yyyy/MM/dd".
For this I tried to use following line inside macro, but it's converting cell data as "###.....#" instead of changing date format.
Sheet1.Range("C3", "C302").NumberFormat = "yyyy/mm/dd"
...
result = "#####...#"
...
Can someone tell me why it's happening? Is there any other way for doing this?
If a date/time cell appears full of # signs, it means that the column is too narrow to display the format.
Make the column wider to accommodate the full width of the selected date format.
See this screenshot. Both columns have the same format. Column A is too narrow to show the dates. Column B is wide enough.
Edit after discussing in chat:
The screen shot you posted in chat is this:
The "dates" you are referring to are not dates. They are numbers that are way higher than what Excel uses for dates in this millenium.
Excel stores dates as whole numbers, starting as 1 for 1/1/1900. What you show in your screenshot are numbers way higher than Excel dates.
Your number 20150930 is NOT what Excel considers Sep-30-2015. For Excel, that date would be the number 42277, which you can perfectly format as that date.
The reason that your "dates" formatted with your format string come out as ##### is that the numbers are way higher than what Excel can interpret as dates.
You will need to convert your numbers to real Excel dates, which you can do with a simple formula. With your first "date" number in cell A1, you can use the formula
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
to return a value that Excel regards as a true date for Sep-30-2015 in this screenshot:
So, the reason for all the # signs is that the numbers you are trying to format as dates are too big for dates in Excel's algorithms.
With all the good answers, I will add simple vba solution...
Option Explicit
Sub FormatDate()
Dim xlRng As Range
Dim xlShtRng As Range
'//- Date format 20160112
Set xlShtRng = [A3:A10] '//- or [A3, A6, A10]
For Each xlRng In xlShtRng
xlRng.Value = DateSerial(Left(xlRng.Value, 4), Mid(xlRng.Value, 5, 2), Right(xlRng.Value, 2))
xlRng.NumberFormat = "yyyy/mm/dd" '//- 2016/01/12
Next
End Sub
Please try this..
=LEFT(A1,4)&"/"&MID(A1,5,2)&"/"&RIGHT(A1,2)

VB.net How to Compare difference in years

New VB coder here, trying to check if todays date is more then 10 years past a date from a grabbed database entry, and display a message if it is.
The Database is already imported and set up in the VB application H ave made, and working, I made a report to display the information.
I am guessing I need to use the Datediff but I can't seem to get it to work, Thanks.
I will give my variable name here
Dim Custsince as Date
'From the Database here
CustSince = CustListodr("Custsince")
Thanks in advance, working with dates is not my strong point.
You can just use a TimeSpan directly by subtracting the dates:
Dim customerLength = DateTime.Now - Custsince
Dim approxYears = customerLength.TotalDays / 365
If the date read from the database is already stored in a Date variable, you can simply subtract one date from another to get the difference. The result of subtracting two dates is a TimeSpan object. TimeSpan objects contain useful properties that allow you to see how long the span of time is in various units (e.g. days, hours, minutes). For instance:
Dim date1 As Date = ...
Dim date2 As Date = Date.Now
Dim span As TimeSpan = date2 - date1
If span.TotalDays >= 3650 then ' Ten years
'...
End If
Alternatively, if you need to compare calendar years, rather than the actual span of time, you can compare the years from each date, like this:
If date2.Year - date1.Year >= 10 Then
'...
End If
If the date being read from the database is stored as a string, rather than as a Date value, you would need to use Date.Parse or Date.ParseExact to convert the string into a Date value.

how to get excel to treat a date as a date not a string when doing CopyFromRecordset

I have an SQL query from SQL Server which returns dates as a string in the format "YYYY-MM-DD".
If I enter a date in this format into a cell, it's recognised as a date.
But when I populate a worksheet with CopyFromRecordset, it seems to be treated as a string.
Any formula which uses the cell converts it to a date first. For example, if my dates are in col A and I make a new column B filled with a formula =A1 + 0
the formula returns my date, as a date.
The problem:
I use the Recordset data for a few things, one of them being a pivot table.
The pivot table does not see my dates as dates. I can't group as dates, for example. My hack is to make a new column which is basically =A1 + 0
I'm going to change my macro to automate this adding a zero, but I wonder if there's a way to get it right from the moment the CopyFromRecordset is performed.
The easiest way would be to do the conversion on the SQL server e.g.
SELECT CAST(date_text AS DATE) FROM TestExcelDates;
CopyFromRecordset is well known for causing data type / cell formatting issues in Excel.
I think I remember reading somewhere this is because the datatype of the recordset is ignored and Excel attempts to work out the format of each column itself based on a subset of the data in the recordset.
The best way round this is to set the cell formatting in the destination range before performing the CopyFromRecordset.
I had this problem after I had changed a view on my SQL Server database. I had changed the data type to DATE; formerly it was on an older version which didn't support DATE so I had used DATETIME. I suspect Excel doesn't always recognize the Date datatype through the SQLOLEDB provider, but it does recognize DATETIME. The field of interest is meas_date. So I altered the view by changing this to a cast SELECT CAST(meas_date AS DATETIME) AS meas_date, ... and refreshed the query in Excel. Worked!
Use the CDate() function when populating cells with dates from the recordset. This will convert the string to a date value.
Edit
That works for setting individual cell values. For using CopyFromRecordset I think you need to do the conversino in the SQL query, so the column returned by the query is a date type rather than a string.
I had this problem too importing data from Teradata, and got around it by first formatting the date columns with NumberFormat = "m/d/yy h:mm;#" (24 hr date) then stepping through the date fields afterwards with VBA and doing ws.cells(iRow, iCol).value = ws.cells(iRow, iCol).value, it forces Excel to reevaluate the string into a date/time field.
This probably will not be the answer but will surely helps you finding the right solution for your problem
String stringCellValue = myCell.toString();
here myCell has datatype as CELL which I've converted to String format.
If u want it in desired Date format, then u can try this-
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
myCellDate = sdf.parse(stringCellValue );
Hope it helps in solving your problem...