Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
excel file 5 53
Macro to Send Appointment from Excel 1 31
Excel VBA - copying formats 7 22
cs.Designer Issue 2 20
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 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

13 Experts available now in Live!

Get 1:1 Help Now