I have a need to add a lot of new rows to an existing list object, say thousands. I noticed that the performance is really bad when performing the operation using code, but it is very fast when doing the action via right-click context menu.
When I select 4000 rows, then right-click on the list object > Insert > Table Rows Above, the 4000 rows were added instantly! So I recorded the macro of this action to see what's being called behind the scenes, and noticed that the ListObject.ListRow.Add is being called 4000 times, like this:
Selection.ListObject.ListRows.Add (2)
Selection.ListObject.ListRows.Add (3)
Selection.ListObject.ListRows.Add (4)
.
.
.
Selection.ListObject.ListRows.Add (4001)
But now, when I execute this macro which I have recorded, it takes about 10 seconds to add 4000 rows to the list object!
Why is there a difference between adding ListRows via right-click and via code?
Even in my C# code, I've disabled ScreenUpdating, disabled EnabledEvents, and set CalculationMode to Manual, but it is still slow.
One might ask, why can't I just use Range.Insert to add the 4000 rows. It's because my user might have some other data at the sides of the list object, and adding new rows to the entire worksheet might corrupt the data. I just want my operations to impact only the list object.
It would be great if someone can tell me what I'm missing to match the performance of adding new rows via right-click context menu.
Thanks!
might corrupt the data. The first part was an advice based on experience i collected working with interop. someone just needs to select a cell in edit mode in an open excel application ... and all interop stops working until you leave the cell (also not works with an open savefile dialog, ...)