简体   繁体   English

Excel vba 插入空白行花费的时间太长

[英]Excel vba to Insert blank rows is taking too long

I have a macro that is comparing 2 cells and inserting a blank row between them if they are different.我有一个宏,它比较 2 个单元格并在它们之间插入一个空白行(如果它们不同)。 It was taking about 12 minutes to complete this process with this code:使用以下代码完成此过程大约需要 12 分钟:

    Worksheets("Dollars").Activate
    Range("B10").Select

'    Do Until ActiveCell.Formula = ""
'        DoEvents
'        If ActiveCell <> ActiveCell.Offset(1, 0) Then
'            ActiveCell.Offset(1, 0).Activate
'            Selection.EntireRow.Insert
'        End If
'        ActiveCell.Offset(1, 0).Activate
'    Loop

I rewrote the code to this way to see if it was any better and it still took over 12 minutes to run.我将代码改写为这种方式,看看它是否更好,但它仍然运行了 12 多分钟。

    Dim r As Long
    Dim vStr1 As String
    Dim vStr2 As String
    r = 10
    vStr1 = ""
    vStr2 = ""

    Do Until Len(Trim(Cells(r, 2))) = 0
        DoEvents
        vStr1 = ""
        vStr2 = ""
        vStr1 = Trim(Cells(r, 2))
        vStr2 = Trim(Cells((r + 1), 2))

        If vStr1 = vStr2 Then
'           do nothing
        Else
            Cells((r + 1), 1).EntireRow.Select
            Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            r = r + 1
        End If
        r = r + 1
    Loop

is there a better way to do this so it doesn't take so long?有没有更好的方法来做到这一点,所以它不需要那么长时间? We are using Windows 10 and Office 2016. Thanks for the help.我们使用的是 Windows 10 和 Office 2016。感谢您的帮助。 I appreciate it....我很感激....

Assuming you only care if A1 <> A2 and so on until the end of your range.... you can use a Union to gather up target cells where you want your rows to be inserted.假设您只关心A1 <> A2等等,直到您的范围结束......您可以使用Union来收集要插入行的目标单元格。 Then, insert the rows all at once at the end rather doing so line by line.然后,在最后一次插入所有行,而不是逐行插入。 Notice that nothing needs to be selected as stated by @BigBen请注意,如@BigBen 所述,无需选择任何内容


Sub Social_Distance()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet2")

Dim lr As Long, MyUnion As Range, xCell As Range
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

For Each xCell In ws.Range("A2:A" & lr)
    If xCell.Value <> xCell.Offset(1).Value Then
        If Not MyUnion Is Nothing Then
            Set MyUnion = Union(MyUnion, xCell.Offset(1))
        Else
            Set MyUnion = xCell.Offset(1)
        End If
    End If
Next xCell

If Not MyUnion Is Nothing Then MyUnion.EntireRow.Insert Shift:=xlDown

End Sub

This will not be tremendously quick, but should do the job.这不会非常快,但应该可以完成工作。

Sub x()

Dim r As Long

Application.ScreenUpdating = False

With Worksheets("Dollars")
    For r = .Range("B" & Rows.Count).End(xlUp).Row To 10 Step -1
        If .Cells(r, 2).Value <> .Cells(r - 1, 2).Value Then
            .Cells(r, 2).EntireRow.Insert
        End If
    Next r
End With

Application.ScreenUpdating = True

End Sub

Generally speaking inserting a lot of rows in Excel is a PITA performance wise.一般来说,在 Excel 中插入很多行是 PITA 性能明智的。
You should consider adding rows at the end of your list and sorting the whole list at the end of the process.您应该考虑在列表末尾添加行并在流程结束时对整个列表进行排序。
I know it's a bit short answer but it's all I can do from my Chromebook now...我知道这是一个简短的答案,但我现在只能通过 Chromebook 做...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM