在其他网站上检查了其他类似问题,但找不到解决方案。
我正在尝试从列表框用户窗体的内容生成变量。列表中的内容是工作簿的名称。第一部分代码向您展示了我如何生成列表的内容供您参考。第二段代码就是有问题的代码。
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的属性,这是我从中得到的。
这是您要尝试的吗?
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句