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
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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