Excel VBA Scroll bar which shift sheet left or right

流过昼夜 提交于 2019-12-06 15:58:00

For an ActiveX scrollbar:

'in the sheet code module
Private Sub ScrollBar1_Change()
    ActiveWindow.ScrollColumn = 4 + Me.ScrollBar1.Value
End Sub

Private Sub ScrollBar1_Scroll()
    ActiveWindow.ScrollColumn = 4 + Me.ScrollBar1.Value
End Sub

Set the scrollbar min to 1 and the max to however many columns you have...

EDIT: if you wanted the scrollbar to move along with the columns then you could do something like this in the Change event (but if you add it to the Scroll event it's going to produce some odd results:

Private Sub ScrollBar1_Change()
    Dim sc As Long
    sc = 4 + Me.ScrollBar1.Value
    ActiveWindow.ScrollColumn = sc
    Me.ScrollBar1.Left = Me.Cells(1, sc).Left
End Sub

BUT - if you then manually move the sheet (via the arrow keys etc) then the scrollbar will move out of view, and there's no sheet_scroll event to catch to use to re-position the scrollbar.

himanshu

please put following code on sheet selection change

Me.ScrollBar1.Value = ActiveWindow.ScrollColumn - 4

please adjust the columns which you want to scroll as fitt to active window then adjust the max scroll value for scrollbar1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!