EXCEL VBA:dblClick,重复代码改进

我有一堆类似构造的用户表单上有许多相同的对象。 对于我的标签(超过50个),我创build了一个dblClick事件来允许用户更改标题。 这工作正常,但我想知道是否有一种方法可以改善我的代码。

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

这是我的代码:

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 

我的用户表单中有超过50个这样的克隆代码行。 我认为有一个更好的方法来做到这一点,但是当我使用类EventHandler 查找时,我无法看到如何将其应用于我的目的。

有没有办法来防止这个重复的代码?

谢谢,

埃利亚斯

 '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