简体   繁体   中英

Get row and column number of first cell in Excel table

I use a lot of tables in my code My table is somewhere in my worksheet. I know I can go to the first cell with the following code:

Worksheets("sheet").ListObjects("table").Range.Cells(1, 1).Activate

But I would like to store the row and column number in 2 integers ie. column = 3 and row = 4 if first cell of table is C4.

Worksheets("sheet").ListObjects("table").Row and Column are not working unfortunately

This prints the row and the column of the first cell of the table:

Public Sub TestMe()

    Dim tbl As ListObject
    Set tbl = Worksheets(1).ListObjects("Table1")
    Debug.Print tbl.Range.Cells(1, 1).Row
    Debug.Print tbl.Range.Cells(1, 1).Column

    'As a bonus:
    Debug.Print tbl.Range.Rows.Count        'total number of rows
    Debug.Print tbl.Range.Columns.Count     'total number of columns

End Sub

Very dirty way, using your code, which is activating the Cells(1,1):

Debug.Print ActiveCell.Row
Debug.Print ActiveCell.Column

You're nearly there. You need:

Worksheets("sheet").ListObjects("table").Range.Cells(1, 1).Row

... to return the absolute row number within the spreadsheet, of your table's first row.

Obviously, the same syntax to return the column number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM