Solved

Excel VBA: find selection in another sheet

Posted on 2007-03-24
5
312 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

749 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