是否有一个更快的代码,用于向表中添加新行 – Excel VBA

我在表中添加一个新行,每次我的代码发现一个新的值添加到它。
我用这个Set newRow = ProjectTable.ListRows.Add的代码工作正常。
但是这使得它运行非常缓慢。

有没有完成相同的代码,但运行速度更快?

 Dim ProjectName As String Dim ResourceType As String Dim newRow As ListRow Dim RPLastRow As Long RPLastRow = RPSheet.Cells(Rows.Count, 1).End(xlUp).Row For Each cell In RPSheet.Range("A5:A" & RPLastRow) If cell = project Then Dim cRow As Long cRow = cell.Row 'enter resource type to table ResourceType = RPSheet.Range("B" & cRow).Value Set newRow = ProjectTable.ListRows.Add newRow.Range(1, 1).Value = ResourceType 'find amount of resources linked to project and add number to table ProjectName = project newRow.Range(1, 2).Value = Sheet2.NumberOfResources(ProjectName, ResourceType) End If Next cell 

编辑:增加了一些额外的代码,所以它更清晰

更快的方法是将数据添加到表的最后,然后调整它的大小。

这是一个例子

 ProjectTable.Resize Range("$A$1:$E$" & lRow) 

哪里是新的最后一行