VBA - How Do I Return to a cell after running a sub

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am running Excel 2007 and writing code that Excel 2003 users may execute.

I need some VBA help. I want my program to remember the address of an ActiveCell. I will then send the program off to execute other code and then I want it to come back to the same cell that was previously active.

I've tried

Code:
Set MyRange = ActiveCell
I've also tried

Code:
Set MyRange = Range.AddressLocal

I keep getting compile errors on both.

Am I on the right track or do I need to try another way?

Thank you for your help in advance.

Charles
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Make sure to Dim the variable properly:

Code:
Public Sub Test()
Dim MyRange As Range
MyRange = ActiveCell
'your code here
MyRange.Select
End Sub

However, most code can be created that does not "select" any cells, since you can work directly with range objects in VBA.
 
Upvote 0
Play with this:
Code:
Sub SomethingsGoBumpInTheNight ()

Dim myRange as Range

Set myRange = Range("A1")
Cells(Rows.Count, Columns.Count).Select
MsgBox "Now to go to myRange as defined in the code"
myRange.Select
MsgBox "Ninja power"

End Sub
 
Upvote 0
I am running Excel 2007 and writing code that Excel 2003 users may execute.

I need some VBA help. I want my program to remember the address of an ActiveCell. I will then send the program off to execute other code and then I want it to come back to the same cell that was previously active.

I've tried

That shouldn't be giving compile errors, unless you have Option Explicit declared at the top of the module...
Then just declare MyRange As Range, as MrKowz shows.

However...
If your subsequent code changes sheets, then you will also need to set a variable to the original sheet, and select it first, then the range..

Code:
Dim MySheet As WorkSheet
Dim MyRange As Range
 
Set MySheet = ActiveSheet
Set MyRange = ActiveCell
 
run some code
 
MySheet.Select
MyRange.Select



However....
Ideally, your best bet is to write your subsequent code in such a way that it does not change which sheet/range is active..
You can write code to manipulate sheets/cells directly without selecting/activating them..


Hope that helps.
 
Upvote 0
How about not leaving the cell that was active when the code was executed?

That could probably be possible, but hard to tell without seeing the code you are running.

If you really want to go back to it try this.
Code:
Set rngActive = ActiveCell
 
' your code
 
Appplication.GoTo rngActive, Scroll
 
Upvote 0
Nice Norie...
I always forget about GoTo Range..
That way the original sheet doesn't need to be active...

Although, I think you misspelled Appplication
 
Upvote 0
No I didn't.

It was the evil keyboard genie.:)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top