How to automatically detect tables in Excel

前端 未结 1 1565
半阙折子戏
半阙折子戏 2021-01-03 12:45

I have a workbook which contains approximately 40 tables. The tables are very disorganised in every file, so you never know where the table might be located in the worksheet

相关标签:
1条回答
  • 2021-01-03 13:06

    Here is some code to iterate through each sheet in a workbook and then go through each cell in each table. The messagebox give the sheet name, table range, table name, value of the cell, row and column . You didn't state where you wanted the information to go, but this will get you the information, you just have to decide where it's going and format it to your requirement.

    Sub FindAllTablesinWB()
        Dim oSh As Worksheet
        Dim oLo As ListObject
        Dim wb As Workbook
        Set wb = ActiveWorkbook
        For Each oSh In wb.Worksheets
    
        For Each oLo In oSh.ListObjects
            For col = 1 To oLo.ListColumns.Count
                For rw = 2 To oLo.ListRows.Count
                  MsgBox "Table: " & oSh.Name & ", " & oLo.Range.address & ", " & oLo.Name & ", " & oLo.Range.Cells(rw, col).Value & ", " & "Row " & rw & " Column " & col
                Next rw
            Next col
        Next oLo
        Next oSh
    End Sub
    
    0 讨论(0)
提交回复
热议问题