How to read an Excel Cell

N

Newbie

Hello,
How to read, from MSP, the content of an Excel cell which is in Sheet1?
Thanks for help

Newbie
 
J

John

Newbie said:
Hello,
How to read, from MSP, the content of an Excel cell which is in Sheet1?
Thanks for help

Newbie

Newbie,
There are several ways to address a particular cell. Here's one
approach. Let's assume you have created an Excel object in your Project
code. For example,
Set Xl = CreateObject("Excel.Application")

If you want to capture the contents of several cells of Excel data on a
particular Worksheet you might want to identify another object. For
example, this defines Workbook "X" (name or index), Worksheet "1".
Set s = Xl.Workbooks(X).Worksheets(1)

You might want to further define an object for a particular starting
cell. For example, cell A2 on the above sheet.
Set c = s.Range("A2")

Now it is fairly easy to set up a loop to read a group of cells. For
example, this loop will read the first three columns in the first 10
rows below cell A2
For nextrow = 1 To 10
firstdata = c.offset(nextrow,0),Value
secdata = c.offset(nextrow, 1).Value
thirddata = c.offset(nextrow, 2).Value
[etc.]
Next

Hope this helps.
John
Project MVP
 
J

Jan De Messemaeker

Hi,

Once Excel is open (see John's post), I prefer using the Cells method

TheWorkbook.Sheets(1).Cells(x,y).value

HTH
 
N

Newbie

Hi John,
Thanks for these detailed explanations !
Newbie

John said:
Newbie said:
Hello,
How to read, from MSP, the content of an Excel cell which is in Sheet1?
Thanks for help

Newbie

Newbie,
There are several ways to address a particular cell. Here's one
approach. Let's assume you have created an Excel object in your Project
code. For example,
Set Xl = CreateObject("Excel.Application")

If you want to capture the contents of several cells of Excel data on a
particular Worksheet you might want to identify another object. For
example, this defines Workbook "X" (name or index), Worksheet "1".
Set s = Xl.Workbooks(X).Worksheets(1)

You might want to further define an object for a particular starting
cell. For example, cell A2 on the above sheet.
Set c = s.Range("A2")

Now it is fairly easy to set up a loop to read a group of cells. For
example, this loop will read the first three columns in the first 10
rows below cell A2
For nextrow = 1 To 10
firstdata = c.offset(nextrow,0),Value
secdata = c.offset(nextrow, 1).Value
thirddata = c.offset(nextrow, 2).Value
[etc.]
Next

Hope this helps.
John
Project MVP
 
J

John

Newbie said:
Hi John,
Thanks for these detailed explanations !
Newbie

Newbie,
You're welcome. I know when I was learning VBA developing the code
became a lot easier after I began to understand the concept of defining
objects. It saves a lot of extra typing. I also found that when working
with VBA in Excel there are a ton of ways to refer cells. My suggestion
and Jan's just scratched the surface.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top