Refer to excel cell in Table by header name and row number (vba)

匿名 (未验证) 提交于 2019-12-03 02:48:02

问题:

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?

回答1:

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) 


回答2:

A shorter answer is:

MsgBox [MyTable].Cells(2, [MyTable[MyColumn]].Column) 

Much cleaner and easier!



回答3:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!