Excel 如何通过标签模拟旋转按钮?

Excel 如何通过标签模拟旋转按钮?,excel,vba,Excel,Vba,我在一张表格上有两个标签 lab01_Click: lab02.Caption = lab02.Caption + 1 有没有办法将lab01用作旋转按钮? 如果我一直按它,那么lab02.Caption应该会不断更改?您也可以在Tag属性中存储数据 乙二醇 更新:好的,我知道如果点击被按下,你想保持它递增。我唯一能想到的是一个令人讨厌的“黑客”重新触发计时器事件。 您需要更新对象名称Userform1、Label1和Label2,并且可能需要将Private Declare函数调整为64位的

我在一张表格上有两个标签

lab01_Click:
lab02.Caption = lab02.Caption + 1
有没有办法将lab01用作旋转按钮?

如果我一直按它,那么lab02.Caption应该会不断更改?

您也可以在Tag属性中存储数据

乙二醇

更新:好的,我知道如果点击被按下,你想保持它递增。我唯一能想到的是一个令人讨厌的“黑客”重新触发计时器事件。 您需要更新对象名称
Userform1
Label1
Label2
,并且可能需要将
Private Declare函数
调整为64位的
Private Declare PtrSafe函数

模块中尝试此操作

Option Explicit

Private Declare Function SetTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long

Private m_TimerID As Long

 'Note:  The duration is measured in milliseconds.
 '         1,000 milliseconds = 1 second
Public Sub StartTimer(ByVal Duration As Long)
     'If the timer isn't already running, start it.
    If m_TimerID = 0 Then
        If Duration > 0 Then
            m_TimerID = SetTimer(0, 0, Duration, AddressOf TimerEvent)
            If m_TimerID = 0 Then
                MsgBox "Timer initialization failed!"
            End If
        Else
            MsgBox "The duration must be greater than zero."
        End If
    Else
        MsgBox "Timer already started."
    End If
End Sub

Public Sub StopTimer()
     'If the timer is already running, shut it off.
    If m_TimerID <> 0 Then
        KillTimer 0, m_TimerID
        m_TimerID = 0
    Else
        MsgBox "Timer is not active."
    End If
End Sub

Public Property Get TimerIsActive() As Boolean
     'A non-zero timer ID indicates that it's turned on.
    TimerIsActive = (m_TimerID <> 0)
End Property

Private Sub TimerEvent()
    If UserForm1.Label2.Tag = "" Then UserForm1.Label2.Tag = 0
    UserForm1.Label2.Tag = CLng(UserForm1.Label2.Tag) + 1
    UserForm1.Label2.Caption = "Increment : " & UserForm1.Label2.Tag
End Sub

也可以在标记属性中存储数据

乙二醇

更新:好的,我知道如果点击被按下,你想保持它递增。我唯一能想到的是一个令人讨厌的“黑客”重新触发计时器事件。 您需要更新对象名称
Userform1
Label1
Label2
,并且可能需要将
Private Declare函数
调整为64位的
Private Declare PtrSafe函数

模块中尝试此操作

Option Explicit

Private Declare Function SetTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long

Private m_TimerID As Long

 'Note:  The duration is measured in milliseconds.
 '         1,000 milliseconds = 1 second
Public Sub StartTimer(ByVal Duration As Long)
     'If the timer isn't already running, start it.
    If m_TimerID = 0 Then
        If Duration > 0 Then
            m_TimerID = SetTimer(0, 0, Duration, AddressOf TimerEvent)
            If m_TimerID = 0 Then
                MsgBox "Timer initialization failed!"
            End If
        Else
            MsgBox "The duration must be greater than zero."
        End If
    Else
        MsgBox "Timer already started."
    End If
End Sub

Public Sub StopTimer()
     'If the timer is already running, shut it off.
    If m_TimerID <> 0 Then
        KillTimer 0, m_TimerID
        m_TimerID = 0
    Else
        MsgBox "Timer is not active."
    End If
End Sub

Public Property Get TimerIsActive() As Boolean
     'A non-zero timer ID indicates that it's turned on.
    TimerIsActive = (m_TimerID <> 0)
End Property

Private Sub TimerEvent()
    If UserForm1.Label2.Tag = "" Then UserForm1.Label2.Tag = 0
    UserForm1.Label2.Tag = CLng(UserForm1.Label2.Tag) + 1
    UserForm1.Label2.Caption = "Increment : " & UserForm1.Label2.Tag
End Sub

哦,没关系,但我认为这并不能解决这个问题:我需要通过点击lab01来模拟旋转按钮。你的代码(和我的一样)在lab02上只做了一次更改,然后我必须一次又一次地单击以达到所需的值。ooo没关系,但我认为这并不能解决问题:我需要通过单击lab01来模拟旋转按钮。您的代码(和我的一样)在lab02上只做了一次更改,然后我必须反复单击以达到所需的值。
Option Explicit

Private Sub Label1_MouseDown(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    StartTimer 200 'millisecond update
End Sub

Private Sub Label1_MouseUp(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    StopTimer
End Sub