VBA - 在mousemove事件中获取标签的名称 [英] VBA - getting name of the label in mousemove event

查看:453
本文介绍了VBA - 在mousemove事件中获取标签的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我在我的工作表中有很多标签,以结构化方式(Label1,Label2,...,Label9)和mousemove-event宏分配给它们。我想在表A1中获得第i个值:A9,其中i是当前被触摸的标签数。在mouse_move事件(或其他一些事件,如Change_event)中有一个简单的方法来获取标签的名称。这将看起来像这样

I have following problem. I have a lot of labels in my worksheet named in a structured way (Label1, Label2, ..., Label9) and mousemove-event macros assigned to all of them. I want to get ith value in table A1:A9, where "i" is a number of label, which is currently "touched". Is there a simple way to get label's name during mouse_move event (or some other events like Change_event). It would be look simmilar to this

Private Sub Label47_MouseMove(ByVal Button As Integer, ByVal Shift As Integer,
                              ByVal X As Single, ByVal Y As Single)

Dim label_name as String
Dim which_one as Integer
Dim val as Integer

label_name = "something like ActiveLabel.Name"
which_one = CInt(right(label_name,1))
val = Cells(which_one,1).Value

                rest code....

End Sub

感谢任何帮助

推荐答案

您可以使用类模块和 WithEvents 来做所需的事情。以下代码应该使您正确的方向:

You can use a class module and WithEvents to do what you need. The following code should get you going in the right direction:

'weMouseMove class module:
Private WithEvents mLbl As MSForms.Label
Private mLabelColl As Collection

Sub LabelsToTrack(Labels As Variant)
    Set mLabelColl = New Collection
    Dim i As Integer
    For i = LBound(Labels) To UBound(Labels)
        Dim LblToTrack As weMouseMove
        Set LblToTrack = New weMouseMove

        Dim Lbl As MSForms.Label
        Set Lbl = Labels(i)

        LblToTrack.TrackLabel Lbl
        mLabelColl.Add LblToTrack
    Next i
End Sub

Sub TrackLabel(Lbl As MSForms.Label)
    Set mLbl = Lbl
End Sub

Private Sub mLbl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
                           ByVal X As Single, ByVal Y As Single)
    MsgBox mLbl.Name & ": " & mLbl.Caption
End Sub

在您的用户形式代码模块中:

And in your userform code module:

Dim MouseMove As New weMouseMove

Private Sub UserForm_Initialize()
    MouseMove.LabelsToTrack Array(Me.Label1, Me.Label2)
End Sub

这篇关于VBA - 在mousemove事件中获取标签的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆