使用macros在logging集中更改后插入多行?

这是一个inheritance的代码,我不是VBA的专家,事实上 – 我从来没有使用它。 在玩了一会之后,我觉得也许有人可以提供一些急需的帮助。

目前这个VBA会在columnd D发生变化的时候添加一行。这个工作正常,所有我想要增加的是插入的行数。 让我们说,而不是1,我想插入4行。 在代码中我会做这些chanegs?

Sub InsertRowAtChangeInValue() Dim lRow As Long ' Break line at USD / Local For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1 If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then Rows(lRow).EntireRow.Insert Next lRow 

我假设它将不得不在Then Rows(lRow).EntireRow.Insert但我所尝试的一切似乎无法正常工作。

如果你也可以解释你为什么build议工作,这也将是有益的,所以我不再需要依靠社区。 谢谢

这是最快的方法:

 Sub InsertRowAtChangeInValue() Dim lRow As Long ' Break line at USD / Local For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1 If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then Rows(lRow).EntireRow.Insert Rows(lRow).EntireRow.Insert Rows(lRow).EntireRow.Insert Rows(lRow).EntireRow.Insert End If Next lRow End Sub