Default300
Board Regular
- Joined
- Jul 13, 2009
- Messages
- 83
Aim
I want to use a Vertical ScrollBar on a Modeless UserForm to scroll the Excel Workbook / Worksheet window in exactly the same way as the (Windows OS) Application window scrollbars move it. Similar speed. Similar proportion (though control length scaled down 6:1). And especially similar smoothness.
The range that I want to scroll up and down is a Data Table which the user can Filter and Sort at will (Advanced Filter, filtered in place).
Summary of Approaches Tried & Problems
I've tried three approaches so far.
Approach (1)
Use SmallChange & LargeChange properties to increment/decrement automatically. Then set ActiveWindow.ScrollRow = VerticalScrollBar.Value every time the ScrollBar_Change event is triggered.
Approach (2)
Change ActiveWindow.ScrollRow using SmallScroll & LargeScroll methods to increment/decrement ActiveWindow.ScrollRow in code, every time ScrollBar_Change event is triggered.
Problem with Approaches (1) & (2)
Smoothness of scroll is drastically impaired when Filter(s) are applied and rows or groups of rows are hidden at random intervals. This results in random pauses where hidden, and jumps where unhidden, for same user input to control.
Approach (3)
Builds on (1) but before setting ActiveWindow.ScrollRow = VerticalScrollBar.Value, checks If ActiveSheet.Rows(VerticalScrollBar.Value).Hidden = True
If it is hidden, it finds the next unhidden row down, using a loop. (The process is sped up somewhat by setting ScrollBar.Delay = 0 for the duration of loop)
Problem with Approach (3)
Smoothness of scroll is somewhat improved, but it will require further refinements.
(a)
When rows are visible, the SmallChange jump is 5 rows.
Whereas if a batch of rows are hidden, then one is visible, then another batch hidden etc... the loop finds the first unhidden and stops there, so it often appears to move down 1 row. Maybe a simple additional For Next loop 1 to SmallChange would do it. May need to take into account any unhidden rows passed between start position and first hidden row too.
(b)
So far have only coded for SmallChange down. Need to detect direction and whether SmallChange or LargeChange. (Perhaps by comparing ScrollBar.Value before and after ScrollBar_Change event and comparing difference to SmallChange & LargeChange.) Then implement 4 Select Case options (1 for each scenario).
Bottom Line
Refinements probably doable, but messy and potentially slow to execute, which may in turn impair smoothness of scroll.
Question
Does anyone know a simple, reliable, non-head wrecking way for an amateur to hook into the application scrollbar parameters?
Windows and/or the Excel application seem to manage to scroll smoothly ignoring hidden rows and just focussing on the visible.
Two interesting leads I've found are:
http://www.xcelfiles.com/APIIndex.html
http://www.cpearson.com/excel/DetectScroll.htm
The latter will also be useful for updating ScrollBar.Value in response to users scrolling page using methods other than my own control (eg keyboard, mousewheel, or windows/application scrollbars).
Any suggestions would be very welcome.
Happy to experiment and learn, but need safe result in (not too long) long run.
I want to use a Vertical ScrollBar on a Modeless UserForm to scroll the Excel Workbook / Worksheet window in exactly the same way as the (Windows OS) Application window scrollbars move it. Similar speed. Similar proportion (though control length scaled down 6:1). And especially similar smoothness.
The range that I want to scroll up and down is a Data Table which the user can Filter and Sort at will (Advanced Filter, filtered in place).
Summary of Approaches Tried & Problems
I've tried three approaches so far.
Approach (1)
Use SmallChange & LargeChange properties to increment/decrement automatically. Then set ActiveWindow.ScrollRow = VerticalScrollBar.Value every time the ScrollBar_Change event is triggered.
Approach (2)
Change ActiveWindow.ScrollRow using SmallScroll & LargeScroll methods to increment/decrement ActiveWindow.ScrollRow in code, every time ScrollBar_Change event is triggered.
Problem with Approaches (1) & (2)
Smoothness of scroll is drastically impaired when Filter(s) are applied and rows or groups of rows are hidden at random intervals. This results in random pauses where hidden, and jumps where unhidden, for same user input to control.
Approach (3)
Builds on (1) but before setting ActiveWindow.ScrollRow = VerticalScrollBar.Value, checks If ActiveSheet.Rows(VerticalScrollBar.Value).Hidden = True
If it is hidden, it finds the next unhidden row down, using a loop. (The process is sped up somewhat by setting ScrollBar.Delay = 0 for the duration of loop)
Problem with Approach (3)
Smoothness of scroll is somewhat improved, but it will require further refinements.
(a)
When rows are visible, the SmallChange jump is 5 rows.
Whereas if a batch of rows are hidden, then one is visible, then another batch hidden etc... the loop finds the first unhidden and stops there, so it often appears to move down 1 row. Maybe a simple additional For Next loop 1 to SmallChange would do it. May need to take into account any unhidden rows passed between start position and first hidden row too.
(b)
So far have only coded for SmallChange down. Need to detect direction and whether SmallChange or LargeChange. (Perhaps by comparing ScrollBar.Value before and after ScrollBar_Change event and comparing difference to SmallChange & LargeChange.) Then implement 4 Select Case options (1 for each scenario).
Bottom Line
Refinements probably doable, but messy and potentially slow to execute, which may in turn impair smoothness of scroll.
Question
Does anyone know a simple, reliable, non-head wrecking way for an amateur to hook into the application scrollbar parameters?
Windows and/or the Excel application seem to manage to scroll smoothly ignoring hidden rows and just focussing on the visible.
Two interesting leads I've found are:
http://www.xcelfiles.com/APIIndex.html
http://www.cpearson.com/excel/DetectScroll.htm
The latter will also be useful for updating ScrollBar.Value in response to users scrolling page using methods other than my own control (eg keyboard, mousewheel, or windows/application scrollbars).
Any suggestions would be very welcome.
Happy to experiment and learn, but need safe result in (not too long) long run.