1

I want to create a index list of all sheets with their names in a table column.

So far I have written the below code but it gives an error on a quoted line.

Dim ws As Worksheet, tbl As ListObject, i As Integer
Set ws = Sheets("Profile Management")
Set tbl = ws.ListObjects("sheets")
With tbl.ListRows
Do While .Count >= 1
.Item(1).Delete
Loop
End With

For i = 1 To Sheets.Count
"tbl.ListColumns(1).DataBodyRange = Sheets(i).Name"
Next I

Where I am going wrong?

4

2 回答 2

1

下面就简单多了。

    Sub GetWorksheetNames()
        Dim i As Long



        ThisWorkbook.Worksheets("Profile Management").Cells(1, 1).Value = "Worksheet Inventory"
        For i = 1 To ThisWorkbook.Worksheets.Count
            ThisWorkbook.Worksheets("Profile Management").Cells(i + 1, 1).Value = ThisWorkbook.Worksheets(i).Name
        Next i
    End Sub
于 2016-03-29T14:21:06.790 回答
0

使用结构化(又名ListObject)表给 VBA 带来了一些额外的问题。您不能以这种方式写入.DataBodyRange 属性,并且 .DataBodyRane 是ListObject的成员,而不是 ListObject 的ListColumns 属性

Option Explicit

Sub wqwe()
    Dim tbl As ListObject, i As Long, w As Long

    With Worksheets("Profile Management")
        With .ListObjects("sheets")
            'make sure there is at least 1 row in the databodyrange
            If .DataBodyRange Is Nothing Then _
                .ListRows.Add
            'clear the first column
            .DataBodyRange.Columns(1).ClearContents
            'insert the worksheet names
            For w = 1 To Worksheets.Count
                'except "Profile Management"
                If Worksheets(w).Name <> .Parent.Name Then
                    i = i + 1
                    'expand the table for new worksheets
                    .DataBodyRange.Cells(i, 1) = Worksheets(w).Name
                    'optionally insert a hyperlink to each worksheet's A1
                    .Parent.Hyperlinks.Add Anchor:=.DataBodyRange.Cells(i, 1), _
                        Address:=vbNullString, SubAddress:=Worksheets(w).Name & "!A1", _
                        TextToDisplay:=Worksheets(w).Name, ScreenTip:="click to go there"
                End If
            Next w
            'reshape the table if there are blank rows
            Do While i < .ListRows.Count
                .ListRows(i + 1).Delete
            Loop
        End With
    End With
End Sub

如上面评论中所述,我添加了直接从表格中的列表中超链接到每个工作表的选项。如果您选择此路线,则不必先将名称放入表格单元格中。

于 2016-03-29T14:56:15.330 回答