vba 如何引用 Excel 表格中的特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16864338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference a specific row in an Excel table
提问by Lukasz
I'm trying to reference a specific row in an excel table, but I get "Application Defined or Object defined error". Here's the line:
我正在尝试引用 Excel 表中的特定行,但出现“应用程序定义或对象定义错误”。这是行:
ActiveWorkbook.Worksheets("MatchedDeals").Cells(i, "A") = _
ActiveWorkbook.Worksheets("Data"). _
Range("Table_ExternalData_1[[" & iStartingRow & "],[TransID]]")
where iStartingRow
contains the row number to reference, and TransID
represents the column. What am I doing wrong?
其中iStartingRow
包含要引用的行号,并TransID
表示列。我究竟做错了什么?
回答by Peter Albert
Instead of addressing the table through a range, try to address the table (in VBA it's a ListObject
):
不要通过范围对表进行寻址,而是尝试对表进行寻址(在 VBA 中它是一个ListObject
):
ActiveWorkbook.Worksheets("MatchedDeals").Cells(i, "A") = _
ActiveWorkbook.Worksheets("Data"). _
ListObjects("Table_ExternalData_1")._
ListColumns("TransID").DataBodyRange(iStartingRow)
回答by Santosh
Try below code
试试下面的代码
Sub sample()
iStartingRow = 2
Set d = Sheets("Data").ListObjects("Table_ExternalData_1")
Worksheets("MatchedDeals").Cells(i, "A") = d.ListRows(1).Range.Cells(iStartingRow, 1)
End Sub