在WB1中,我使用上面的代码使用User表单在Lisbox1中显示已打开的工作簿名称及其在listbox2中的各自工作表。但我想使用Listview1和Listview2,因为我希望每个工作簿和工作表名称在每个工具簿和工作表名称旁边显示一个复选框,我应该做哪些更改,因此它适用于Listview1和Listview2。
Option Explicit
Private Sub UserForm_Initialize()
Dim wb As Workbook
Me.Caption = "Workbooks and Sheets Detail"
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
End Sub
Private Sub ListBox1_Click()
Dim sWorkbookname As String
sWorkbookname = ListBox1.List(ListBox1.ListIndex)
ListWbWorksheets sWorkbookname
End Sub
Private Sub ListWbWorksheets(ByVal psWorkbookName As String)
Dim targetWb As Excel.Workbook
Dim n As Long
Set targetWb = Application.Workbooks(psWorkbookName)
ListBox2.Clear
For n = 1 To targetWb.Sheets.Count
ListBox2.AddItem targetWb.Sheets(n).Name
Next n
Set targetWb = Nothing
End Sub
答案 0 :(得分:2)
它将尝试一些试验和错误来学习如何使用Listview。这应该会给你一个良好的开端。
Private Sub ListView1_Click()
Dim ws As Worksheet
Dim item As ComctlLib.ListItem
ListView2.ListItems.Clear
For Each ws In Workbooks(ListView1.SelectedItem.Text).Worksheets
Set item = ListView2.ListItems.Add(Text:=ws.Name)
Next
End Sub
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim item As ComctlLib.ListItem
With ListView1
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Workbooks"
.ColumnHeaders.Add Text:="Paths"
End With
With ListView2
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Worksheets"
End With
For Each wb In Workbooks
Set item = ListView1.ListItems.Add(Text:=wb.Name)
item.SubItems(1) = wb.Path
Next
End Sub