?
Solved

Excel VBA: find selection in another sheet

Posted on 2007-03-24
5
Medium Priority
?
339 Views
Last Modified: 2008-01-09
Using VBA, how do i find out what the currently selected range in another sheet is (without activating that sheet).  

I run my sub (macro) from sheet1 and i would like to find out which cell is selected in sheet2.  The only way i know how to do that now is to activate sheet2 and use activecell or selection but this causes the screen to flash to sheet2 for a moment (before i switch back) which i cannot do.  What is the correct way to find this info?
0
Comment
Question by:dave4dl
  • 3
  • 2
5 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 18786182
There is not way to do without activating the worksheet or using VBA code to store the active cell somewhere when each worksheet is deactivated. The easiest way to solve the problem is to turn screen updating off, activate the sheet, get the active cell, re-activate the original sheet, and then turn screen updating back on.

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18786191
Something like this:

   Dim OriginalWorksheet As Worksheet
   Dim OtherActiveCell As Range
   
   Application.ScreenUpdating = False
   Set OriginalWorksheet = ActiveSheet
   TargetWorksheet.Activate
   Set ActiveCell = ActiveCell
   OriginalWorksheet.Activate
   Application.ScreenUpdating = True

Kevin
0
 
LVL 15

Author Comment

by:dave4dl
ID: 18786249
Kevin,
Thanks for the posts.  I will leave this one open for another few days or so just to see if anyone out there knows a way of doing this without activating the sheet.

Thanks,

-Dave
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18787258
Dave,

Here is the only other way to do this. Place the following code in each worksheet code module:

Public Property Get Selection() As Range
   Set Selection = Me.Range(Me.Names("Selection"))
End Property

Place this code in the ThisWorkbook code module:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
   Sh.Names.Add "Selection", Target
End Sub

Then, to access the selection in any worksheet at any time regardless of whether or not that worksheet is active, use the following code:

   Set OtherWorksheetSelection = Sheets("Sheet1").Selection

Note that each worksheet must be activated and the selection changed once for the setting for that worksheet to take effect. Once set the setting is persistent, even after saving and reopening the workbook.

Kevin
0
 
LVL 15

Author Comment

by:dave4dl
ID: 18826767
Thanks a bunch for your help!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
In this post, we will learn to set up the Group Naming policy and will see how it is going to impact the Display Name and the Email addresses of the Group.
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.
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…

580 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