Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Storing a User's Location before Running a Macro.

Storing a User's Location before Running a Macro

Written by Allen Wyatt (last updated March 20, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


John has a macro that does some processing on various worksheets in a workbook. He wants, at the beginning of the macro, to save the range of cells (or the single cell) that the user has selected. He uses ActiveCell.Address to determine this. Then, at the end of the macro, he wants to return to the user with the same range selected that they originally had selected.

The problem is, the macro could be finished on an entirely different worksheet than where the user started, and ActiveCell.Address only gives a cell address, not a worksheet name and definitely not a range. John wonders about the best way to store what he needs so he can return to the user's original location at the end of the macro.

For the best chance of getting someone back to where they started, there are three elements: workbook, worksheet, and cell. Actually, this last element (cell) may be a bit simplistic, as the user will always have a cell selected (this is what is returned by ActiveCell.Address), but may additionally have a range selected.

Here's how you get all four items:

Dim rngOrigSelection As Range
Dim rngOrigCell As Range
Dim sOrigWS As String
Dim sOrigWB As String

Set rngOrigSelection = Selection
Set rngOrigCell = ActiveCell
sOrigWS = ActiveSheet.Name
sOrigWB = ActiveWorkbook.Name

When you want to later return the user to where they were, you can use this type of code:

Workbooks(sOrigWB).Activate
Sheets(sOrigWS).Select
rngOrigSelection.Select
rngOrigCell.Activate

Of course, Excel always has multiple ways that you can accomplish any given task. In this case, you could shorten your code by only remembering the active cell and selected range:

Dim rngOrigSelection As Range
Dim rngOrigCell As Range

Set rngOrigSelection = Selection
Set rngOrigCell = ActiveCell

When you want to restore the user to the location, you rely upon the Parent object available in VBA:

rngOrigSelection.Parent.Parent.Activate
rngOrigSelection.Parent.Select
rngOrigSelection.Select
rngOrigCell.Activate

The Parent object of the selection range you saved is the worksheet in which that range is located, and the Parent of that Parent object is the workbook in which the worksheet is located.

Another approach is to simply create, within your macro, a named range that refers to whatever the user had selected:

ActiveWorkbook.Names.Add Name:="MyOrigPlace", RefersTo:=Selection

After you do your processing, when you are ready to return to what the user had selected, you use this code:

Application.Goto Reference:="MyOrigPlace"
ActiveWorkbook.Names("MyOrigPlace").Delete

The first line returns to the selection and the second line then deletes the named range. The only drawback to this approach is that the active cell is not retained; the code assumes that you want the upper-left cell in the range to be the active cell when it is done. You should also be aware that if your processing deletes the cells that make up the named range, then the code may not work properly—Excel can't go to a place that no longer exists.

Of course, you may not have to remember any location at all, if you code your macro correctly. While VBA allows you to "move around" and select different areas of your worksheets and workbook, in most cases this isn't necessary. You could, for instance, simply work with different ranges and then do your work on those ranges, without ever changing the current selection or active cell. Indeed, VBA allows you to change, reformat, sort, delete, and do almost anything you can imagine to cells without actually needing to select them.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6161) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Storing a User's Location before Running a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Ordering Worksheets Based on a Cell Value

Need to sort your worksheets so that they appear in an order determined by the value of a cell on each worksheet? Using a ...

Discover More

Combining First and Second Numbered Levels on One Paragraph

Want to customize your paragraph numbering in Word? There are a few tricks that can be used to automatically display the ...

Discover More

Moving a Table Row

Want to move a row in a table very easily? You can do so by using the same editing techniques you are already using.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Copying Worksheets in a Macro

Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...

Discover More

Checking if a Workbook is Already Open

Knowing if a workbook is already open can be a prerequisite to your macro working correctly. Here's how to check it out.

Discover More

Determining an Integer Value

When creating macros, you often need to process numbers in various ways. VBA allows you to convert a numeric value to an ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.