2

I've been looking up in Google in these past days but I still can't find a good answer to this one.

Currently, this is how I do it:

For Each cell In ws.Range(fromCol, toCol)
    If IsNothing(cell.Value) Then Exit For

    valueList.Push(cell.Value.ToString())
Next

But when it reads a cell whose assumed data type is Time, it returns a Double value. I try to parse that value but it's not the same as expected.

How can I properly read a single Excel cell with an assumed type of Time?

mr5
  • 3,438
  • 3
  • 40
  • 57
  • Have you read this SO post?: [What is the difference between .text, .value, and .value2?](http://stackoverflow.com/questions/17359835/what-is-the-difference-between-text-value-and-value2) – Bjørn-Roger Kringsjå Feb 15 '15 at 10:11
  • @Bjørn-RogerKringsjå Thanks for the link but in my case, `Value` and `Value2` returns the same result for that cell I'm talking about. However, according to the top answers in that link, Text` is bad but I think I'm going for it as I can't see any flaw as of now. – mr5 Feb 15 '15 at 10:27
  • possible duplicate of [Reading Datetime value From Excel sheet](http://stackoverflow.com/questions/4538321/reading-datetime-value-from-excel-sheet) – Esselans Feb 15 '15 at 10:30
  • @mr5 Did you try `DateTime.FromOADate` as suggested in the link above? – Bjørn-Roger Kringsjå Feb 15 '15 at 10:46

1 Answers1

0

As per the comment suggesting the article,

.Text is a bad idea in the sense that it will give you just the displayed string in that cell. If you use TypeName on the cell's content, for example, it will always return string regardless of any datatypes the content might be. However, if you use .Value, it will return the actual datatype name of the content of that cell.

This can prove useful to you if you're using TypeName in a comparison for instance. It saves you using conversion functions.

Take this example:

Sub Test()
Range("A1") = "True"
Debug.print(TypeName(Range("A1").Value))
Debug.print(TypeName(Range("A1").Text))
End Sub 

This Output is:

Boolean
String
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51