Excel 更改对列表框的引用以添加项目

Excel 更改对列表框的引用以添加项目,excel,vba,listbox,Excel,Vba,Listbox,我有一个userform,里面有多个列表框和一个用于添加项的按钮。 当用户单击按钮时,他可以按名称选择列表框并添加新事件(现在是静态类型)。当用户试图保存它时,代码将调用函数,该函数应将引用更改为应在其中添加和创建新项的列表框 我看到这样的草稿,但我不知道如何处理这个listboxnames(参见注释): 您有解决方案或参考资料吗?是否可以解释 就我个人而言,我会用我的两种形式来处理这个问题 我的主窗体名为UserForm1,我的“事件选择器”名为EventPicker Option Expli

我有一个userform,里面有多个列表框和一个用于添加项的按钮。 当用户单击按钮时,他可以按名称选择列表框并添加新事件(现在是静态类型)。当用户试图保存它时,代码将调用函数,该函数应将引用更改为应在其中添加和创建新项的列表框

我看到这样的草稿,但我不知道如何处理这个listboxnames(参见注释):


您有解决方案或参考资料吗?是否可以解释

就我个人而言,我会用我的两种形式来处理这个问题

我的主窗体名为
UserForm1
,我的“事件选择器”名为
EventPicker

Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
    Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
    Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
    Dim lBox As MSForms.ListBox
    Dim lbl As MSForms.Label
    Dim ctrl As Control

    Debug.Print Me.NamePickerCombo.Value, Me.TextBox1.Value

    For Each ctrl In ParentForm.Controls
        If TypeName(ctrl) = "Label" Then
            If ctrl.Caption = Me.NamePickerCombo.Value Then
                Set lbl = ctrl
                Exit For
            End If
        End If
    Next ctrl

    For Each ctrl In ParentForm.Controls
        If ctrl.Left >= lbl.Left And ctrl.Left + ctrl.Width <= lbl.Left + lbl.Width Then
            Set lBox = ctrl
            Exit For
        End If
    Next ctrl

    lBox.AddItem Me.TextBox1.Value

    Unload Me
End Sub
Private Sub CommandButton2_Click()
    Unload Me
End Sub
UserForm1
被设置为您的示例

EventPicker
显示

在我的
UserForm1
后面,我有以下代码

Option Explicit
Private Sub CommandButton1_Click()
    Dim ctrl
    Dim Eventfrm As EventPicker

    Set Eventfrm = New EventPicker
    Set Eventfrm.ParentForm = Me

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "ListBox" Then
            Eventfrm.NamePickerCombo.AddItem ctrl.Name
        End If
    Next ctrl

    Eventfrm.Show
End Sub
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
    Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
    Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
    Dim lBox As MSForms.ListBox

    Set lBox = ParentForm.Controls(Me.NamePickerCombo.Value)
    lBox.AddItem Me.TextBox1.Value

    Unload Me
End Sub
Private Sub CommandButton2_Click()
    Unload Me
End Sub
这是在“添加事件”按钮的
上单击
,并通过填充
组合框
并设置
父窗体
属性来启动
事件选择器
窗体,以便子窗体知道它来自何处

在我的
EventPicker
表单后面,我有以下代码

Option Explicit
Private Sub CommandButton1_Click()
    Dim ctrl
    Dim Eventfrm As EventPicker

    Set Eventfrm = New EventPicker
    Set Eventfrm.ParentForm = Me

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "ListBox" Then
            Eventfrm.NamePickerCombo.AddItem ctrl.Name
        End If
    Next ctrl

    Eventfrm.Show
End Sub
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
    Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
    Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
    Dim lBox As MSForms.ListBox

    Set lBox = ParentForm.Controls(Me.NamePickerCombo.Value)
    lBox.AddItem Me.TextBox1.Value

    Unload Me
End Sub
Private Sub CommandButton2_Click()
    Unload Me
End Sub
留给我以下的

更新以使用名称而不是控件
Name
s

UserForm1之后

Option Explicit

Private Sub CommandButton1_Click()
    Dim ctrl
    Dim Eventfrm As EventPicker

    Set Eventfrm = New EventPicker
    Set Eventfrm.ParentForm = Me

    Debug.Print Me.Name, TypeName(Me)

    For Each ctrl In Me.Controls
        Debug.Print ctrl.Name, TypeName(ctrl)
        If TypeName(ctrl) = "Label" Then
            Eventfrm.NamePickerCombo.AddItem ctrl.Caption
        End If
    Next ctrl

    Eventfrm.Show
End Sub
事件选择器后面的
EventPicker

Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
    Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
    Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
    Dim lBox As MSForms.ListBox
    Dim lbl As MSForms.Label
    Dim ctrl As Control

    Debug.Print Me.NamePickerCombo.Value, Me.TextBox1.Value

    For Each ctrl In ParentForm.Controls
        If TypeName(ctrl) = "Label" Then
            If ctrl.Caption = Me.NamePickerCombo.Value Then
                Set lbl = ctrl
                Exit For
            End If
        End If
    Next ctrl

    For Each ctrl In ParentForm.Controls
        If ctrl.Left >= lbl.Left And ctrl.Left + ctrl.Width <= lbl.Left + lbl.Width Then
            Set lBox = ctrl
            Exit For
        End If
    Next ctrl

    lBox.AddItem Me.TextBox1.Value

    Unload Me
End Sub
Private Sub CommandButton2_Click()
    Unload Me
End Sub
选项显式
专用vParentForm作为UserForm1
公共属性集ParentForm(v作为UserForm1)
设置vParentForm=v
端属性
公共属性获取ParentForm()作为UserForm1
Set ParentForm=vParentForm
端属性
私有子命令按钮1_单击()
将lBox设置为MSForms.ListBox
作为MSForms.Label的Dim lbl
按ctrl键作为控件
Debug.Print Me.NamePickerCombo.Value,Me.TextBox1.Value
对于ParentForm.Controls中的每个ctrl
如果TypeName(ctrl)=“标签”,则
如果ctrl.Caption=Me.NamePickerCombo.Value,则
设置lbl=ctrl
退出
如果结束
如果结束
下一个ctrl键
对于ParentForm.Controls中的每个ctrl

如果ctrl.Left>=lbl.Left和ctrl.Left+ctrl.Width而不是将
ListBoxName
作为
String
传递,为什么不直接传递
ListBox
?另外,在您的示例中,似乎无论如何都无法选择要将其添加到Hi-Tom的
ListBox
,感谢anserw。按下“添加事件”按钮后,新的用户窗体出现,用户可以在其中选择他将选择的列表框(按名称)(我使用了带有名称的复选框,以便从中获取字符串)。我怎样才能参考那里的列表框?传递列表框会容易得多,但我无法想象如何做..从新的userform如何将输入传递回父userform?因此我需要将所有列表框传递给子userform,让用户选择一个,并在另一个由父用户表单编写的函数中将所选列表框传递回父表单,该函数从子用户表单调用?非常感谢!这真的很有帮助!