Solved

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

Posted on 2012-03-15
3
233 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:
DaFranker 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 33

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

730 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