Excel VBA:ListBox-UserFormvariables创build问题

检查了其他网站上的其他类似问题,但无法find解决scheme。

我正在尝试从listbox-userform的内容生成variables。 列表中的内容是工作簿的名称。 第一段代码展示了如何生成列表的内容供您参考。 第二个代码是有问题的代码。

Private Sub CommandButton1_Click() Dim wb As Workbook For Each wb In Workbooks With ListBox1 .AddItem (wb.Name) End With Next wb lbl_Exit: End Sub 

我在下面的每一行上得到一个Object Required错误。 这段代码驻留在用户Userform

 Private Sub CommandButton3_Click() Dim MainBook As Workbook Dim ListBox1 As ListBox For Each Item In ListBox1 If Item.Name Like "Aggregate" Then Item.Select Set MainBook = Selection End If Next End Sub 

:我读了这个Item是一个ListBox的属性,这是我从那里得到的。

这是你正在尝试?

 Private Sub CommandButton1_Click() Dim wb As Workbook For Each wb In Workbooks With ListBox1 .AddItem (wb.Name) End With Next wb End Sub Private Sub CommandButton2_Click() Dim MainBook As Workbook, i as Long For i = 0 To (ListBox1.ListCount -1) If ListBox1.List(i) Like "Aggregate" Then Set MainBook = Workbooks(ListBox1.List(i)) MainBook.Activate Exit For End If Next End Sub