VB Macro to Align Worksheets with Top Worksheet (134829)



The information in this article applies to:

  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q134829
This article also applies to:
  • Microsoft Excel for Windows 95 7.0|7.0
  • Microsoft Excel for Windows 5.0c|5.0c
  • Microsoft Excel for Windows NT 5.0|5.0
  • Microsoft Excel for the Macintosh 5.0|5.0
  • Microsoft Excel for the Macintosh 5.0a|5.0a

SUMMARY

When worksheets have been grouped and a cell selected, that cell is then the active cell on each of the grouped sheets. In order to view that cell, each sheet must be individually manipulated to bring the active cell into view. The following is a sample Microsoft Visual Basic for Applications macro (Sub procedure) to align all the worksheets this way.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Sample Visual Basic Procedure

    Sub AlignSheets()

       ' Remember which sheet was on top.
       Set TopSheet = ActiveSheet

       ' Do not update the screen until the end of the routine.
       Application.ScreenUpdating = False

       ' Store the sheet's position.
       TopRow = ActiveWindow.VisibleRange.Row
       LeftColumn = ActiveWindow.VisibleRange.Column

       ' Loop through each worksheet in the total worksheets.
       For Each xPage In Worksheets

           ' Activate worksheet.
           xPage.Activate

           ' Scroll the active sheet to the stored position.
           ActiveWindow.ScrollRow = TopRow
           ActiveWindow.ScrollColumn = LeftColumn

       ' Get the next worksheet.
       Next xPage

       ' Restore the original sheet.
       TopSheet.Activate

   End Sub
				

REFERENCES

"Visual Basic User's Guide," version 5.0, Chapter 5, "Working with Objects in Visual Basic"

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming KB134829