[Excel VBA] How to Display Two Sheets of the Same Workbook Side-by-Side

It is common to work across multiple sheets in a single Excel workbook, such as moving between an input sheet and a summary sheet, or a data sheet and its corresponding chart.

While you can manually use Excel’s “New Window” and “Arrange All” features to display two sheets side-by-side, doing this every time is tedious. Using VBA, you can perform this entire sequence with a single button click.

In this article, I will explain the VBA code for displaying different sheets of the same workbook in two windows side-by-side.


目次

Completed VBA Code

Below is the VBA code that displays the first sheet and the last sheet of the currently open workbook side-by-side.

Sub ArrangeSheetsSideBySide()

    ' Declare variables
    Dim targetBook As Workbook
    Set targetBook = ThisWorkbook
    
    ' Check if there are at least two sheets in the workbook
    If targetBook.Worksheets.Count < 2 Then
        MsgBox "This macro requires at least two sheets.", vbExclamation
        Exit Sub
    End If
    
    ' --- 1. Create a new window ---
    ' This results in two windows for the same workbook
    targetBook.NewWindow
    
    ' --- 2. Arrange only the windows of this workbook side-by-side ---
    targetBook.Windows.Arrange ArrangeStyle:=xlArrangeVertical
    
    ' --- 3. Switch the displayed sheet in each window ---
    ' Display the first sheet in the first window
    targetBook.Windows(1).Activate
    targetBook.Worksheets(1).Activate
    
    ' Display the last sheet in the second window
    targetBook.Windows(2).Activate
    targetBook.Worksheets(targetBook.Worksheets.Count).Activate
    
    ' Return focus to the original window (left side) (Optional)
    targetBook.Windows(1).Activate
    
    MsgBox "Sheets are now displayed side-by-side."

End Sub

Key Points of the Code

1. Creating a New Window: .NewWindow

targetBook.NewWindow

Executing the .NewWindow method of the Workbook object duplicates the display window for that workbook. This creates two windows for the same file (e.g., MyBook.xlsx:1 and MyBook.xlsx:2). Note that the file itself is not duplicated; it simply creates an additional “viewing portal.”

2. Arranging Windows: .Arrange

targetBook.Windows.Arrange ArrangeStyle:=xlArrangeVertical

The Windows.Arrange method is a command to organize open windows.

  • targetBook.Windows.Arrange: By calling this from the Workbook object, you target only the windows belonging to that specific workbook. Other open Excel files remain unaffected.
  • ArrangeStyle:=xlArrangeVertical: Displays windows side-by-side (split vertically). Setting this to xlArrangeHorizontal would stack them vertically.

3. Activating Windows and Sheets

targetBook.Windows(1).Activate targetBook.Worksheets(1).Activate

To display specific sheets in each window after arranging them, a two-step process is required: “First activate the window, then activate the sheet within it.”

  • targetBook.Windows(1).Activate: Activates the first window belonging to the workbook.
  • targetBook.Worksheets(1).Activate: Displays the first worksheet within that active window.

In the sample code, the Worksheets.Count property is used to dynamically specify the last sheet for the second window.


How to Return to the Normal View

Returning to a single-window view is very simple. Just click the “Close (X)” button in the top-right corner of either window.

Closing one window does not close the workbook (file) itself. The remaining window will automatically maximize and return to the standard view.


Summary

The procedure to display two sheets side-by-side involves these three steps:

  1. Use .NewWindow to increase the viewing windows of the same workbook to two.
  2. Use .Windows.Arrange to align those windows side-by-side (or top-to-bottom).
  3. Activate each window by its index number and activate the desired sheet within it.

By registering this macro to your Quick Access Toolbar, you can significantly improve work efficiency even in workbooks with complex sheet structures.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次