【问题标题】:Excel VBA move select items from listbox to a listbox in a differnt form that will be another popup userformExcel VBA 将选择项从列表框移动到不同表单的列表框,该表单将成为另一个弹出用户表单
【发布时间】:2015-04-15 05:20:38
【问题描述】:

我有一个工作簿,其中有一个列表框,人们可以选择多行。我想在选择后将这些信息以新的形式带到另一个 ListBox 中,一旦选择就会弹出。

我发现了在同一用户表单中的列表框之间传输的示例,但我需要帮助在不同表单之间移动它们。

这段代码展示了如何在同一个表单上的两个用户表单之间传递信息。但我需要修改它以在两种不同的形式之间移动它。就像您单击提交按钮并弹出另一个带有文本框的列表框一样。

Option Explicit
'Move Listbox Items in UserForm
'code from Dave Peterson
'posted on www.contextures.com


    Private Sub BTN_moveAllLeft_Click()

    Dim iCtr As Long

    For iCtr = 0 To Me.ListBox2.ListCount - 1
        Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
    Next iCtr

    Me.ListBox2.Clear
End Sub


Private Sub BTN_moveAllRight_Click()

Dim iCtr As Long

For iCtr = 0 To Me.ListBox1.ListCount - 1
    Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
Next iCtr

Me.ListBox1.Clear
End Sub


Private Sub BTN_MoveSelectedLeft_Click()

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1
    If Me.ListBox2.Selected(iCtr) = True Then
        Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
    End If
Next iCtr

For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1
    If Me.ListBox2.Selected(iCtr) = True Then
        Me.ListBox2.RemoveItem iCtr
    End If
Next iCtr

End Sub


Private Sub BTN_MoveSelectedRight_Click()

Dim iCtr As Long

For iCtr = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(iCtr) = True Then
        Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
    End If
Next iCtr

For iCtr = Me.ListBox1.ListCount - 1 To 0 Step -1
    If Me.ListBox1.Selected(iCtr) = True Then
        Me.ListBox1.RemoveItem iCtr
    End If
Next iCtr

End Sub

Private Sub cmdOK_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()

Dim iCtr As Long

With Me.ListBox1
    For iCtr = 1 To 10
        .AddItem "This is a test" & iCtr
    Next iCtr
End With

With Me.ListBox2
    For iCtr = 1 To 10
        .AddItem "This is a not a test" & iCtr
    Next iCtr
End With

Me.ListBox1.MultiSelect = fmMultiSelectMulti
Me.ListBox2.MultiSelect = fmMultiSelectMulti

End Sub

【问题讨论】:

    标签: excel userform vba


    【解决方案1】:

    无论您在哪里看到 Me,您都可以引用不同的用户表单。

    例如,我有两个用户窗体 UserForm1 和 UserForm2。假设我们的初始列表框ListBox1 在UserForm1 上,并且包含一个颜色列表。 UserForm1 还包含一个命令按钮 CommandButton1,它将把选定的项目从 UserForm1 移动到 UserForm2 中的不同列表框(也称为 ListBox1)到下一个表单。

    UserForm1 中的代码,显示的初始形式,命令按钮上的代码如下所示:

    Private Sub CommandButton1_Click()
    
        Dim i As Integer
    
        For i = 0 To Me.ListBox1.ListCount - 1
            If Me.ListBox1.Selected(i) = True Then
                UserForm2.ListBox1.AddItem Me.ListBox1.List(i)
            End If
        Next i
    
        UserForm2.Show
    
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多