
Tips.Net > ExcelTips Home > General > Maintaining the Active Cell
Summary: Move from one worksheet to another, and Excel selects whatever cell was last used in the worksheet you are selecting. If you don’t want this behavior (you want to have the same cell selected on the new worksheet as on the old), then you can apply the techniques in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
An Excel workbook can contain any number of individual worksheets. As you move around within the various worksheets, Excel keeps track of which cell is selected in which worksheet. When you switch to a new worksheet, Excel makes active the cell that was last active within that worksheet. Thus, if you last selected cell F9 in the worksheet, that is the one that is selected when you display the worksheet again, regardless of what was selected in the previous worksheet.
For some workbooks, however, you may want Excel to make the active cell in the selected worksheet the same as the active cell in the previous worksheet. There is no setting to automatically do this in Excel, but there are a couple of things you can try. One thing is to follow these steps:
These steps work because you are "grouping" worksheets. When you do, Excel makes the selected cells the same for all worksheets in the group.
Remembering to use the Ctrl-click-click-Ctrl sequence can be cumbersome, at best. It is also a sequence that can be fraught with danger, because if you forget to do step 3, you could end up making unintended changes on your worksheets. (While you are working with grouped worksheets, any change you make on one sheet is similarly changed on all the sheets in the group.)
These three steps cannot be automated with macros, but you can take a different approach with a macro to accomplish the same task. The first thing you need to do is declare a public variable anywhere within a module of the workbook, as shown here:
Public sAddress As String
This variable, sAddress, will be used to store the current address of the active cell. In the "ThisWorkbook" module of the workbook, add these two macros:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
sAddress = ActiveCell.Address
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
If sAddress > "" Then Sh.Range(sAddress).Select
End Sub
The first macro is run automatically by Excel any time that the selected cell changes. All it does is retrieve the address of whatever cell is active, and then store that address in the sAddress variable.
The second macro is automatically run whenever a workbook is activated. It checks to see if there is anything stored in sAddress. If there is, it selects whatever cell address is stored there. The error code is necessary in case you select a sheet that doesn't use cells, such as a chart sheet.
This macro approach works great if you only want to make this navigational change in a single workbook or two. If you prefer to make the change "system wide" (so to speak), you must be a little more complex in your approach to the macro. In this case, you need to place your code in the Personal.xls workbook so that it is loaded every time you start Excel. Specifically, place the following code into a new class module of the Personal.xls workbook. This class module should be named something descriptive, such as ClassXLApp:
Public WithEvents gobjXLApp As Excel.Application
Private mstrAddress As String
Private Sub gobjXLApp_WorkbookActivate(ByVal Wb As Excel.Workbook)
On Error Resume Next
If mstrAddress > "" Then ActiveSheet.Range(mstrAddress).Select
End Sub
Private Sub gobjXLApp_SheetActivate(ByVal Sh As Object)
On Error Resume Next
If mstrAddress > "" Then Sh.Range(mstrAddress).Select
End Sub
Private Sub gobjXLApp_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
mstrAddress = Selection.Address
End Sub
Next, open the "ThisWorkbook" module of Personal.xls and copy the following code to it:
Private mobjXLApp As New ClassXLApp
Private Sub Workbook_Open()
Set mobjXLApp.gobjXLApp = Excel.Application
End Sub
Once you save Personal.xls and restart Excel, the range in the first workbook that opens will be selected in the next worksheet that is selected.
Tip #3205 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
Check out ExcelTips: The Macros today!
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Bugs and Pests Tips
ExcelTips
Family Tips
Health Tips
Home Tips
Organizing Tips
WordTips
Advertise on the
ExcelTips Site