Link to home
Start Free TrialLog in
Avatar of dave4dl
dave4dl

asked on

Excel VBA: find selection in another sheet

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?
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of dave4dl
dave4dl

ASKER

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
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
Avatar of dave4dl

ASKER

Thanks a bunch for your help!