I'm trying to refer to a cell in an excel table by using the table header name and the row number using VBA.
Is this posible?
I'm trying to refer to a cell in an excel table by using the table header name and the row number using VBA.
Is this posible?
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)
A shorter answer is:
MsgBox [MyTable].Cells(2, [MyTable[MyColumn]].Column)
Much cleaner and easier!
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)