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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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("Selecti on"))
End Property
Place this code in the ThisWorkbook code module:
Private Sub Workbook_SheetSelectionCha nge(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
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("Selecti
End Property
Place this code in the ThisWorkbook code module:
Private Sub Workbook_SheetSelectionCha
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
ASKER
Thanks a bunch for your help!
Dim OriginalWorksheet As Worksheet
Dim OtherActiveCell As Range
Application.ScreenUpdating
Set OriginalWorksheet = ActiveSheet
TargetWorksheet.Activate
Set ActiveCell = ActiveCell
OriginalWorksheet.Activate
Application.ScreenUpdating
Kevin