Solved

Excel VBA: find selection in another sheet

Posted on 2007-03-24
5
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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