2015-04-21 56 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