Restore the position in the window using VBA in Microsoft Excel

If you want to restore the screen picture for the user after a macro has changed the window view,
you can sometimes just activate the cell that was the active cell when the macro started. This is not always successful.
The following example code will restore the windowposition just like it was when the macro started :

Dim aRow As Long, aColumn As Integer, aRange As String ' global variables
Sub RememberWindowPosition() ' run this before making changes
    With ActiveWindow
        aRow = .ScrollRow
        aColumn = .ScrollColumn
    End With
    aRange = Selection.Address
End Sub

Sub RestoreWindowPosition() ' run this to restore position in the window
    With ActiveWindow
        .ScrollRow = aRow
        .ScrollColumn = aColumn
    End With
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.