我有以下问题。我的工作表中有很多标签以结构化方式命名(Label1,Label2,...,Label9)和分配给所有这些标签的mousemove-event宏。我想在表A1中获得ith值:A9,其中“i”是标签的数量,当前是“触摸”的。是否有一种简单的方法可以在mouse_move事件(或其他一些事件,如Change_event)中获取标签名称。这看起来很简单
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
感谢您的帮助
答案 0 :(得分:2)
您可以使用课程模块和WithEvents
来完成您的需要。以下代码可以帮助您朝着正确的方向前进:
'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
在您的用户表单代码模块中:
Dim MouseMove As New weMouseMove
Private Sub UserForm_Initialize()
MouseMove.LabelsToTrack Array(Me.Label1, Me.Label2)
End Sub