This post may contain affiliate links. Please read my disclosure for more info:

Manipulating and changing scroll bars with VBA

VBA Code Snippets

VBA Code Snippets

When recording a macro, Excel includes all the scroll bar movement in the code.  Normally, we delete this code, as we just don’t need it.  But occasionally, being able to control the scroll bars and their display properties is useful.

 

Display or Hide Scroll Bars

Scroll bars can be displayed or hidden.  It is an application level setting, so will be applied to all workbooks open within the application.

The code below shows how to hide or display the scroll bars for all workbooks open in the application.

'Display all Scroll Bars
Application.DisplayScrollBars = True
'Hide all Scroll Bars
Application.DisplayScrollBars = False

The below shows how to hide or display either the horizontal or vertical scroll bars for active workbook.

'Hide all the vertical or horizontal Scroll Bar
ActiveWindow.DisplayHorizontalScrollBar = False
ActiveWindow.DisplayVerticalScrollBar = False
'Display the vertical or horizontal Scroll Bar
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True

 

Fix the Scroll Area

Fixing the scroll area will prevent a user from scrolling into or selecting any of the cells outside of the specified range.  I use this with dashboards so that the user’s screen is always focused on the key information, even if they try to scroll it won’t let them.

'Set the scroll area to a specific range
ActiveSheet.ScrollArea = "A10:D20"
'Reset the scroll area
ActiveSheet.ScrollArea = ""

 

 

Scroll to specific location on the worksheet

The code below shows how to scroll to a specific row or column.  Change the ScrollRow and ScrollColumn values to meet your requirements.

'Scroll to a specific row and column
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 10

 

Scroll by a specific number of rows or columns

SmallScroll and LargeScroll can be used to scroll the window, using the active cell as a start point.  The good news is that even if the code tries to scroll the horizontal or vertical position to be less than Row 1 or Column A the code will not create an error.

'Scroll horizontally or vertically by a specific number of rows or columns
ActiveWindow.SmallScroll Up:=100
ActiveWindow.SmallScroll Down:=50
ActiveWindow.SmallScroll ToRight:=8
ActiveWindow.SmallScroll ToLeft:=8
'The SmallScroll method can be applied within a single line
ActiveWindow.SmallScroll Up:=20, ToRight:=10
'The SmallScroll method can be applied without referencing the direction.
'The arguments must be presented in the order show below
'ActiveWindow.SmallScroll ([Down], [Up], [ToRight], [ToLeft])
ActiveWindow.SmallScroll 20, , 30,
'LargeScroll also exists as a method.  LongScroll will scroll a page at a time,
'rather than individual row/column
ActiveWindow.LargeScroll Down:=2, ToLeft:=1

The values can be negative numbers.  Up:=-20 is equivalent to Down:=20.  The same is true for ToLeft and ToRight.

 

What Next?

Wouldn’t it be great to know how to programme VBA, rather than piecing together bits of code from different websites?  If this is how you feel, maybe you should consider purchasing Excel 2016 Power Programming with VBA.  Click the image to purchase your copy from Amazon.

Leave a Reply

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