Solved

how to read the cell values In Other Sheets Without Activating or Selecting First

Posted on 2012-03-15
3
235 Views
Last Modified: 2012-04-03
how to read the cell values In Other sheets or from macro without Activating or Selecting the Sheet2


          sheets("Sheet2").cells(3,2).value

----  folllowing works but I don't want to activate the sheet2

Sheets("Sheet2").Activate
Sheets("Sheet2").cells(3,2).value
0
Comment
Question by:Bharat Guru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
Frank White earned 150 total points
ID: 37726207
This doesn't seem to be related to C# at all, unless your question is about how to do this in C# to interface with Excel's Object Model specifically. Your question seems more to be about which methods and objects to use in the Object Model itself, independently of which language is used to access them.

In any case: To access cell values from a worksheet while another worksheet or workbook is activated, a usually solid method is to declare a worksheet variable (Dim wsMySheet as Worksheet), and then set this variable to Workbooks("WorkbookFilename.xls").Sheets("Sheet2"), and from there use wsMySheet.Cells().Value whenever you want to access cells from this worksheet.

This allows you to reuse the code more easily by simply changing the set assignation of the variable, while also saving you some typing in most cases.

There are many situations where you have to explicitly state the workbook and then the sheet before you can access ranges in that sheet, with varying reasons.
0
 
LVL 13

Expert Comment

by:Shanan212
ID: 37726246
In addition to DaFranker,

It would be


Dim report as worksheet, result as worksheet

Set report = Sheets("Report")
Set result = Sheets("Result")

Then you can use it like this

report.cells(3,2).value = result.cells(5,2).value
0
 
LVL 34

Expert Comment

by:Norie
ID: 37726278
This on it's own should work in VBA, though you should add a workbook reference.
Sheets("Sheet2").Cells(3,2).Value

Open in new window


If you are using C# you can use this, where xlwb is a reference to a workbook
 xlwb.Worksheets["Sheet1"].Cells[3,2].Value

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question