reading excel data

shankar

Member
Joined
Dec 14, 2002
Messages
12
Hello,
I have the following code to read excel data from my VB.Net application.

Dim oXLApp As Excel.Application
oXLApp = New Excel.Application()
oXLApp.Workbooks.Open("fileName")

And now I try to access a cell as
Line 1
oXLApp.Range("cell1").Row where cell1 = cell name


Suppose, I change the cell name from cell1 to cell2 in my excel file, even then Line 1 executes correctly. I thought it would throw an exception because I have changed the cell name.
(Observation : In the excel file, I get a drop down in the name field just below the save button, it contains all the names that I have entered for the cells. And the above line works fine for all the names in the drop down, albeit some of the names are not being used by any cell)

Am i missing something when using cell names instead of A-1 style way of accessing cells ??

Shankar
 
Hi,

if you want to access the cells correctly, follow the Excel object-hierarchy. This example shows you the access to a single cell:

Dim oXLApp As Excel.Application
Dim oXLWs As Excel.Worksheet
dim strCell as String

oXLApp = New Excel.Application()
oXLApp.Workbooks.Open("FileName")
oXLWs = oXLApp.ActiveWorkbook.Sheets(1)
strCell = oXLWs.Range("A1").Value

or

strCell = oXLWs.Range("Name_of_the_cell").Value

To access more than one cell, take a for-next-statement etc. combined with the cells-object.
 
Last edited by a moderator:
Back
Top