retain Selection

D

DrFear

I'm trying to restore the Selection that was in place at the time I started a
routine that changes the selection during its run. In other words I want
something like

dim saveSel as Range
Set saveSel = Application.Selection

.... do stuff that destroys the current selection then

saveSel.Select

And the problem with this is of course that the object to which saveSel
refers has been destroyed in the meantime. What I actually want is to have
saveSel be a range (or ref to a range) of cells that is set to whatever was
selected originally.

Any ideas?
 
G

Gary''s Student

It should work exactly as you have coded it. If, however, you are navigating
between workbook/worksheets then save that information as well.
 
J

Jim Cone

Dim saveSel as String
saveSel = Selection.Address
'do stuff
Range(saveSel).Select
(only works on active sheet)
--
Jim Cone
Portland, Oregon USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)




DrFear" <[email protected]>
wrote in message
I'm trying to restore the Selection that was in place at the time I started a
routine that changes the selection during its run. In other words I want
something like

dim saveSel as Range
Set saveSel = Application.Selection
.... do stuff that destroys the current selection then
saveSel.Select

And the problem with this is of course that the object to which saveSel
refers has been destroyed in the meantime. What I actually want is to have
saveSel be a range (or ref to a range) of cells that is set to whatever was
selected originally.
Any ideas?
 
D

DrFear

Should work perhaps ... but doesn't. I wasn't clear enough: after changing
the selection, I actuallyu delete the columns containing the cells originally
selected before repopulating. So in my code saveCell does not even point to
Nothing, it just errors to object not existing.

I do navigate to another workbook during ... steps, but at the time of my
unsuccessful saveSel.Select, ActiveWorkbook and ActiveSheet have been
restored to where the original selection was.

It still seems to me the problem is Set does not create a range object but
creates a pointer to a range object, and that object gets destroyed.

If I could get the range of Application.Selection expressed as a text string
I could later restore it with Range(storedString).Select - but how would I
get that text string in the first place, and is this not a rather clumsy way
of doing things?

Thanks for any further suggestions!
 
D

DrFear

Thanks - exactly what I needed (see my redundant reply to Gary's Student,
crossed in the post)
 
N

Norman Jones

Hi Dr Fear,


Try something like

'==========>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim rCell As Range

Set WB = ActiveWorkBook
Set Rng = Selection
Set rCell = ActiveCell

' \\ Your code

Application.Goto Reference:=Rng
rCell.Activate

End Sub
'<==========
 
N

Norman Jones

Hi Dr Fear,

Stripping the code to that which is
essential, try:


'==========>
Public Sub Tester()
Dim Rng As Range
Dim rCell As Range

Set Rng = Selection
Set rCell = ActiveCell

' \\ Your code

Application.Goto Reference:=Rng
rCell.Activate

End Sub
'<==========
 
D

Dave Peterson

If you're changing workbooks and or worksheets, you'll have to activate those
first.

savesel.parent.parent.activate 'activate the workbook
savesel.parent.select 'select the worksheet
savesel.select 'select the range

or you could use:

application.goto savesel, scroll:=true 'or false???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top