本文介绍了是否有用于将新行添加到表的更快的代码-Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我的代码找到要添加到表中的新值时,我都会在表中添加新行.
我用于此 Set newRow = ProjectTable.ListRows.Add 的代码工作正常.
但这会使它运行很慢.

I add a new row to a table, everytime my code finds a new value to add to it.
The code I use for this Set newRow = ProjectTable.ListRows.Add works fine.
but this makes it run very slow.

有没有可以实现相同但运行速度更快的代码?

Is there a code that accomplishes the same but runs quicker?

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

添加了一些额外的代码,使其更加清晰

Added some extra code so its more clear

推荐答案

更快的方法是将数据添加到表的末尾,然后简单地调整其大小.

The faster way is to add data to the end of the table and then simply resize it.

这是一个例子

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

lRow 是新的最后一行

这篇关于是否有用于将新行添加到表的更快的代码-Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:46