vba 从集合中将数据写入 listobject(性能)
vba write data into listobject from collection (performance)
我在将数据写入 table (listobject)
时遇到了低性能问题
我有一个对象集合,想将它们逐行写入 table
For Each msrecord In mls.mlRecords
Dim oNewRow As ListRow
Set oNewRow = Sheets(mls.name).ListObjects(1).ListRows.Add(AlwaysInsert:=True)
oNewRow.Range.Cells(1, 1).Value = msrecord.id
Next msrecord
然而这太慢了。我试过 Application.ScreenUpdating = False 但没有成功。
我的想法是先填充一个数组,然后一次性全部写入table。
Dim outputArray As Variant
Dim counter As Long
For counter = 1 To mls.mlRecords.Count
outputArray(counter, 1).Value = msrecord.id
outputArray(counter, 2).Value = msrecord.name
outputArray(counter, 3).Value = msrecord.morefields
Next
Sheets(mls.name).ListObjects(1).DataBodyRange.Value = outputArray
但是,我现在 运行 在变量数组中赋值时出现类型不匹配错误。我不能真正重新调整数组,因为我有一个集合可以开始。
有什么想法吗?先建立数组有意义吗?如何有效地将数据写入 table (listobjects)?
在调用 Redim 之前,您的变量不是数组。在此之前,它是 Empty,这是 Variant 的默认值。尝试在循环之前添加它。
ReDim outputArray(1 to mls.mlRecords.Count, 1 to 3)
我在将数据写入 table (listobject)
时遇到了低性能问题我有一个对象集合,想将它们逐行写入 table
For Each msrecord In mls.mlRecords
Dim oNewRow As ListRow
Set oNewRow = Sheets(mls.name).ListObjects(1).ListRows.Add(AlwaysInsert:=True)
oNewRow.Range.Cells(1, 1).Value = msrecord.id
Next msrecord
然而这太慢了。我试过 Application.ScreenUpdating = False 但没有成功。
我的想法是先填充一个数组,然后一次性全部写入table。
Dim outputArray As Variant
Dim counter As Long
For counter = 1 To mls.mlRecords.Count
outputArray(counter, 1).Value = msrecord.id
outputArray(counter, 2).Value = msrecord.name
outputArray(counter, 3).Value = msrecord.morefields
Next
Sheets(mls.name).ListObjects(1).DataBodyRange.Value = outputArray
但是,我现在 运行 在变量数组中赋值时出现类型不匹配错误。我不能真正重新调整数组,因为我有一个集合可以开始。
有什么想法吗?先建立数组有意义吗?如何有效地将数据写入 table (listobjects)?
在调用 Redim 之前,您的变量不是数组。在此之前,它是 Empty,这是 Variant 的默认值。尝试在循环之前添加它。
ReDim outputArray(1 to mls.mlRecords.Count, 1 to 3)