Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Viewing Same Cells on Different Worksheets

When using multiple worksheets, Chris wonders if there is a way to 'lock' the scrolling through all worksheets. For instance, if he scrolls down and across on Sheet1 until rows 100 to 140 and columns G to P are in view, then when he switches to Sheet2 (or any other worksheet) he would like the same rows and columns to be shown on those worksheets.

The only way to accomplish this task is through the use of macros. What has to happen is that the macro needs to determine which rows and columns are visible when a sheet is deactivated (being left) and then set the display of the activated sheet (the one you are going to) to the same rows and columns. The following macros, added to the ThisWorkbook module, perform exactly this task.

Dim grngSelection As Range
Dim gintScrollColumn As Integer
Dim glngScrollRow As Long

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If TypeName(ActiveSheet) = "Worksheet" Then
        On Error Resume Next
        With ActiveWindow
            Sh.Range(grngSelection.Address).Select
            .ScrollColumn = gintScrollColumn
            .ScrollRow = glngScrollRow
        End With
    End If
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim oSheet As Object
    If TypeName(Sh) = "Worksheet" Then
        Set oSheet = ActiveSheet
        Application.EnableEvents = False
        Sh.Activate
        With ActiveWindow
            gintScrollColumn = .ScrollColumn
            glngScrollRow = .ScrollRow
            Set grngSelection = .RangeSelection
        End With
        oSheet.Activate
        Application.EnableEvents = True
    End If
End Sub

Note the use of the variables outside of the event handlers. These variables are used to pass the values of the column, row, and selected area from the SheetDeactivate handler to the SheetActivate handler.

Of course, you may not want an automatic solution. Instead, you may want the user to take a specific step to trigger whether the worksheets are synchronized. This can be done by adding the following macro to a regular module in your workbook:

Global WindowScrollRow
Global WindowScrollCol
Global WindowSyncOn As Boolean

Public Sub WindowLock()
    If Not WindowSyncOn Then
        WindowScrollRow = ActiveWindow.VisibleRange.Row
        WindowScrollCol = ActiveWindow.VisibleRange.Column
        Application.StatusBar = "WindowSync: ON"
    Else
        Application.StatusBar = ""
    End If
    WindowSyncOn = Not WindowSyncOn
End Sub

All that this macro does is to check the status of the global variable WindowSyncOn. If the value is False, then the current settings for the top visible row and leftmost visible column are stored into global variables. The setting of these variables are then used by the following event handler, added to the ThisWorkbook module:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If WindowSyncOn Then
        If Not ActiveWindow Is Nothing Then
            ActiveWindow.ScrollRow = WindowScrollRow
            ActiveWindow.ScrollColumn = WindowScrollCol
        End If
    End If
End Sub

The macro simply checks the setting of the WindowSyncOn variable, and if it is True (it has been set), then the macro sets which row and column are at the top and left of the active window.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3860) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Ted Skandy    24 Apr 2014, 15:10
I have a worksheet with hyperlinks and email addresses. I'd like to move certain entries to a new sheet. I have gone through and highlighted the entries to move with a different highlight color. Is it possible to identify all of the entries with the same color and mover them to another worksheet?
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.