Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
excelvba:dblClick,重复代码改进_Excel_Vba_Events_Userform_Repeat - Fatal编程技术网

excelvba:dblClick,重复代码改进

excelvba:dblClick,重复代码改进,excel,vba,events,userform,repeat,Excel,Vba,Events,Userform,Repeat,我有一堆构造类似的用户表单,上面有许多相同的对象。对于我的标签(数字超过50),我创建了一个dblClick事件,允许用户更改标题。这一切都很好,但我想知道是否有办法改进我的代码 有没有一种方法可以避免仅仅为了处理不同对象上的同一事件而复制同一代码的几十个副本 这是我的密码: Private Sub Item1_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean) Item1_Label.Caption = InputBox(&quo

我有一堆构造类似的用户表单,上面有许多相同的对象。对于我的标签(数字超过50),我创建了一个dblClick事件,允许用户更改标题。这一切都很好,但我想知道是否有办法改进我的代码

有没有一种方法可以避免仅仅为了处理不同对象上的同一事件而复制同一代码的几十个副本

这是我的密码:

Private Sub Item1_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
    Item1_Label.Caption = InputBox("blah?", "blah", Item1_Label.Caption)
        if Item1_Label.Caption = "" Then Item1_Label.Caption = "Item 1"
End Sub

Private Sub Item2_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
    Item2_Label.Caption = InputBox("blah?", "blah", Item2_Label.Caption)
        if Item2_Label.Caption = "" Then Item2_Label.Caption = "Item 2"
End Sub

Private Sub Item3_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
    Item3_Label.Caption = InputBox("blah?", "blah", Item3_Label.Caption)
        if Item3_Label.Caption = "" Then Item3_Label.Caption = "Item 3"
End Sub

'etcetera etcetera
我的userform中有50多行这样的克隆代码。我认为有更好的方法可以做到这一点,但当我开始使用类EventHandler时,我不知道如何将其应用于我的目的

有没有办法防止这种重复的代码

谢谢


Elias

您发布的链接应该适用于标签…谢谢。嗯,到目前为止,我有了一个良好的开端(我必须研究“收藏”和“clsLabel”)。但问题是双击操作不起作用。你能告诉我我做错了什么吗?嗯,这对我很有用。。。很难说你的代码版本有什么问题。您是否尝试过调试以确保集合已填充?我已在即时窗口中键入“print colLabels(1)”,但这不起作用。我对班级模块非常了解;在过去的30分钟里,我学到了前所未有的东西。不过,我还是无法让它发挥作用。(Excel 2013)如果在
Set l.oLabel=o
上添加断点,则加载表单时是否输入断点?或者在该行之后添加
Debug.print o.Name
,查看即时窗口中的内容。是的,“Debug.print o.Name”正在工作。它只打印出表单中每个标签的名称。此外,断点在“Set l.oLabel=o”行停止。我的clsLabel有问题吗?
'clsLabel
Public WithEvents oLabel As MSForms.Label

Public Sub oLabel_dblClick(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox oLabel.Caption
End Sub



'form code
Private colLabels As Collection

Private Sub UserForm_Initialize()
Dim o As Control, l As clsLabel
Set colLabels = New Collection
For Each o In Me.Controls
    If TypeName(o) = "Label" Then
        Set l = New clsLabel
        Set l.oLabel = o
        colLabels.Add l
    End If
Next o
End Sub