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

how to read the cell values In Other sheets or from macro without Activating or Selecting the Sheet2


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

Bharat GuruAsked:
Who is Participating?
Frank WhiteConnect With a Mentor Commented:
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.
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
NorieVBA ExpertCommented:
This on it's own should work in VBA, though you should add a workbook reference.

Open in new window

If you are using C# you can use this, where xlwb is a reference to a workbook

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.