Refer to Excel cell in Table by header name and row number

后端 未结 7 1478
孤独总比滥情好
孤独总比滥情好 2020-12-23 14:58

I\'m trying to refer to a cell in an excel table by using the table header name and the row number using VBA.

How can I do this?

相关标签:
7条回答
  • 2020-12-23 15:03

    Is there any reason one should avoid using this method?

    ThisWorkbook.Worksheets("MyWksht").Range("TableName[ColumnTitle]").Cells(RowNumber)

    Seems the simplest answer in my opinion.

    0 讨论(0)
  • 2020-12-23 15:07

    It seems to me that @Salam Morcos solution will not give a proper answer. If table starts from cell A2 statment [MyTable[FirstColumnName]].Column would give value of 2. Proper solution would be:

    MsgBox [MyTable].Cells(2, [MyTable].Column-[MyTable[MyColumn]].Column + 1)
    
    0 讨论(0)
  • 2020-12-23 15:17

    Much simpler, and dynamic:

    ThisWorkbook.Worksheets("WorkSheet_Name").Range("Table_Name").Rows(Table_row_number).Select
    
    0 讨论(0)
  • 2020-12-23 15:19

    In your example, something like this:

    Dim tb As ListObject
    'assumes Table is the first one on the ActiveSheet
    Set tb = ActiveSheet.ListObjects(1)
    MsgBox tb.DataBodyRange.Cells(2, tb.ListColumns("header4").Index)
    
    0 讨论(0)
  • 2020-12-23 15:20

    It is as simple as this. If you call by the name of the column, then the column index will be 1 for that range so:

    msgbox ActiveSheet.ListObjects(1).ListColumns("header4").DataBodyRange(2,1).value
    
    0 讨论(0)
  • 2020-12-23 15:21

    If you want to write it short:

    [MyTable[header4]].Rows(2)
    
    0 讨论(0)
提交回复
热议问题