1

在其他网站上查看了其他类似的问题,但找不到解决方案。

我正在尝试从列表框用户表单的内容中生成变量。列表中的内容是工作簿的名称。第一段代码向您展示了我如何生成列表的内容以供您参考。第二段代码是有问题的。

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下面的 For Each 行中遇到错误。这段代码位于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 的一个属性,这是我从中得到的。

4

1 回答 1

0

这是你正在尝试的吗?

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
于 2013-09-20T17:49:48.567 回答