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 = ""
Generate accurate VBA code in seconds with AutoMacro
AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities.
Whether you’re an experienced coder looking to save time, or a newbie just trying to get things to work, AutoMacro is the tool for you.
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.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: