Solved

Excel VBA: find selection in another sheet

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now