Solved

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

Posted on 2012-03-15
3
229 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now