zxzxzx
zxzxzx

Reputation: 33

Using VB.net to get Excel Cell Value which is not the correct value?

I've been stuck on this for the past few hours so I really could do with some help. I am trying to get numeric (integer and decimal) values from an Excel spreadsheet and use it create a graph on my form using the chart control or even display that value in a textbox.

The Value of cell A11 is "2003" and K11 is "12.00" but the returned values are 0 and 12. I use formulas to calculate the rest of the y values. The strange thing is that text works fine. Cell A3 has "initial" which comes back as "initial", it's just the numbers which don't work.

    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open("C:\File.xls")    
    xlWorkBook = xlApp.Workbooks.Add("C:\File.xls")
    xlSheet = xlWorkBook.Worksheets(1)

    Dim x1 As String
    Dim y1 As String
    x1 = xlSheet.Range("A11").Value.ToString     
    y1 = xlSheet.Range("K11").Value.ToString
    MsgBox(x1)

Thanks in advance

Upvotes: 0

Views: 19370

Answers (1)

SysDragon
SysDragon

Reputation: 9888

Try using .Text instead of .Value. It will retrieve the value shown instead of the cell value:

x1 = xlSheet.Range("A11").Text
y1 = xlSheet.Range("K11").Text

This will ignore even the formats, for example, the value 7.7777 in a cell with a format to show 2 decimal places will show 7.78, and that is what .Text will retrieve.

Upvotes: 1

Related Questions