用 VBA 字段按字段填充 Excel 表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23596415/
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
Fill Excel Table with VBA Field by Field
提问by sharkantipav
I have an Excel table, that I need to fill record by record.
我有一个 Excel 表格,我需要按记录填写。
I began a code
我开始写代码
Private Sub AddRecBro_Click()
Dim Tbl As ListObject
Dim NewRow As ListRow
Set Tbl = Worksheets("Broker").ListObjects("Broker")
Set NewRow = Tbl.ListRows.Add(AlwaysInsert:=True)
....
End Sub
Now I would like to populate this table field by field a bit like this...
现在我想按字段填充这个表字段有点像这样......
Field("ID")=Max(Broker[ID])+1
Field("F1")=Sheets("Dashboard").Range("U5").Value
Field("F2")=Sheets("Dashboard").Range("U6").Value
Field("F3")=Sheets("Dashboard").Range("U7").Value
etccc
I don't know how to populate my list objects this way...Actually I have my data in column and not at the same place... so i would like to select them at their location to Fill up the New record... Thanks
我不知道如何以这种方式填充我的列表对象......实际上我的数据在列中而不是在同一个地方......所以我想在它们的位置选择它们以填写新记录.. 。 谢谢
回答by David Zemens
A ListObject
table is comprised of a HeaderRowRange
(if the table has headers) and a DataBodyRange
. Both are Range
objects that you can iterate using normal iteration methods for cells:
一个ListObject
表由一个HeaderRowRange
(如果表有标题)和一个DataBodyRange
. 两者都是Range
您可以使用单元格的常规迭代方法进行迭代的对象:
Dim r as Long, c as Long
For r = 1 to Tbl.DataBodyRange.Rows.Count
For c = 1 to Tbl.DataBodyRange.COlumns.Count
Tbl.DataBodyRange.Cells(r, c).Value = "__Insert some value__"
Next
Next