2015-04-21 57 views
0

我在用戶窗體中有一個滾動條,如果向左移動並運行一些不同的代碼(如果向右移動),我想運行一些代碼。VBA - 如果滾動條左右更改

像這樣的東西(這顯然不工作):

Private Sub ScrollBar1_Change() 

If ScrollBar1 = Left Then 

MsgBox "left" 

ElseIf ScrollBar1 = Right Then 

MsgBox "right" 

End If 

End Sub 

謝謝

回答

0

您需要使用一個模塊級變量來跟蹤的最後位置,並將其與當前:

Private mCurrentScrollPos As Long 

Private Sub ScrollBar1_Change() 
    If (ScrollBar1.Value > mCurrentScrollPos) Then 
     MsgBox "Left" 
    Else 
     MsgBox "Right" 
    End If 

    mCurrentScrollPos = ScrollBar1.Value 
End Sub 
+0

歡呼隊友,完美 – krismosel

0

您需要存儲全局變量。當用戶窗體被激活時設置它。而這樣做的代碼

Private Sub ScrollBar1_Change() 
    If (ScrollBar1.Value > scrollLoc) Then 
     MsgBox "Left" 
    Else 
     MsgBox "Right" 
    End If 

    scrollLoc= ScrollBar1.Value 
end sub 

Private Sub UserForm_Activate() 

    scrollLoc = Me.ScrollBar1.Value 
End Sub 

,並有一個全局變量

private scrollLoc as long