Introduction
There are times when you want to move the actual Excel window position during a VBA macro execution. For example, you might want to display Excel side-by-side with another application or present specific information in a prominent position for the user.
Using VBA, you can move the window to any location by directly specifying the screen coordinates (.Left, .Top).
In this article, I will explain a polite and reliable method that changes the window position and then restores it to the user’s original setting after the macro finishes.
Completed VBA Code
Below is the VBA code that temporarily stores the current state and position of the window, moves it to the top-left of the screen, and then restores it to its original state.
Sub MoveAndRestoreWindow()
' Declare variables
Dim originalState As Long
Dim originalLeft As Double
Dim originalTop As Double
' --- 1. Memorize the original window state and position ---
With ActiveWindow
originalState = .WindowState
originalLeft = .Left
originalTop = .Top
End With
' --- 2. Change the window position ---
MsgBox "Moving the window to the top left of the screen."
With ActiveWindow
' To change the position, WindowState must be set to "Normal"
.WindowState = xlNormal
' Set the new position (Top-Left corner of the screen)
.Left = 0
.Top = 0
End With
' --- 3. Restore the original window position ---
MsgBox "Restoring the window position."
With ActiveWindow
' Set the memorized original position
.Left = originalLeft
.Top = originalTop
' Finally, restore the original state (Maximized, etc.)
.WindowState = originalState
End With
MsgBox "The window state has been restored."
End Sub
Key Points of the Code
1. Memorizing the Original State and Position
With ActiveWindow
originalState = .WindowState
originalLeft = .Left
originalTop = .Top
End With
It is good macro etiquette to first store all information regarding the ActiveWindow‘s current state and position in variables so as not to permanently alter the user’s working environment.
.WindowState: Retrieves whether the window is “Maximized,” “Minimized,” or “Normal.”.Left/.Top: Retrieves the distance from the left/top edge of the screen to the top-left corner of the window in “points.”
2. Setting WindowState to “Normal” (xlNormal)
.WindowState = xlNormal
This is a mandatory step for changing the window’s position or size. The position properties (.Left, .Top) can only be set when the window state is “Normal” (neither maximized nor minimized). Since you cannot directly move a maximized window, you must switch to this state first.
3. Setting the Position (.Left / .Top)
.Left = 0
.Top = 0
After setting the window state to xlNormal, assign values to the .Left and .Top properties. This moves the top-left corner of the window to the specified screen coordinates. Left = 0, Top = 0 in the sample code means the top-left corner of the screen.
4. Restoring the Original State
.Left = originalLeft
.Top = originalTop
.WindowState = originalState
After the main processing of the macro is complete, use the variables stored in step 1 to restore the window’s position (Left, Top) and state (WindowState). This allows the user to resume work smoothly in the exact same environment as before the macro ran.
Summary
The safe and recommended procedure for manipulating window positions in VBA is as follows:
- At the beginning of the process, store the current
.WindowState,.Left, and.Topin variables. - Change the window state to
.WindowState = xlNormal. - Set the target
.Leftand.Topcoordinates. - (Execute the main processing of the macro)
- At the end of the process, use the stored variables to restore the original position and state.
By practicing this “Memorize and Restore” pattern, you can create helpful and stable macros that respect the user’s working environment.
