简体   繁体   English

以更有效的方式根据单元格值删除两个不同工作表上的行 [VBA Excel]

[英]Delete rows on two different sheets based on cell value in a more efficient way [VBA Excel]

I have a two different worksheets with the same number of rows each one.我有两个不同的工作表,每个工作表的行数相同。 In column RI have "New" or "Old" depending on the row (this is a dynamic value).在列 RI 中有“新”或“旧”,具体取决于行(这是一个动态值)。 What I want to do is, if a row in Worksheet1 contains "Old" in column R, then delete that row in both Worksheet1 and Worksheet2.我想要做的是,如果 Worksheet1 中的一行在 R 列中包含“旧”,则在 Worksheet1 和 Worksheet2 中删除该行。

Now, I have tried two codes for this:现在,我为此尝试了两个代码:

Dim w1 As Worksheet
Dim w2 As Worksheet

Set w1= Worksheets("Sheet1")
Set w2= Worksheets("Sheet2")
'-----------------------------------------------------
'Code 1
'-----------------------------------------------------
Application.ScreenUpdating = False
 For r = w1.UsedRange.Rows.Count To 1 Step -1
     If Cells(r, "R") = "Old" Then
         w1.Rows(r).EntireRow.Delete
         w2.Rows(r).EntireRow.Delete
     End If
 Next r
 Application.ScreenUpdating = True
'-----------------------------------------------------
'Code 2
'-----------------------------------------------------

Dim i As Long

i = 1
Application.ScreenUpdating = False
Do While i <= w1.Range("R1").CurrentRegion.Rows.Count

If InStr(1, w1.Cells(i, 18).Text, "Old", vbTextCompare) > 0 Then
    w1.Cells(i, 1).EntireRow.Delete
    w2.Cells(i, 1).EntireRow.Delete
Else
    i = i + 1
End If

Loop
Application.ScreenUpdating = True

Usually I have +800 rows, so Code 1 works as desired but it sometimes takes too long, like 3 minutes.通常我有 +800 行,所以代码 1 按需要工作,但有时需要太长时间,比如 3 分钟。 Code 2 gets stuck so far.到目前为止,代码 2 卡住了。

What is an efficient way of doing this?这样做的有效方法是什么?

Delete Rows In Sheets删除工作表中的行

Implementing Union should considerably speed up the process.实施联盟应大大加快这一进程。

The Code编码

Sub DeleteRowsInSheets()

    Const cSheet1 As Variant = "Sheet1"    ' First Worksheet Name/Index
    Const cSheet2 As Variant = "Sheet2"    ' First Worksheet Name/Index
    Const cVntCol As Variant = "R"         ' Search Column Letter/Number
    Const cStrCriteria As String = "Old"   ' Search Criteria String

    Dim rngU1 As Range   ' Union Range 1
    Dim rngU2 As Range   ' Union Range 2
    Dim LastUR As Long   ' Last Used Row
    Dim i As Long        ' Row Counter

    With Worksheets(cSheet1)

        ' Calculate Last Used Row.
        If .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
                Is Nothing Then Exit Sub
        LastUR = .Cells.Find("*", , , , , 2).Row

        ' Add found cells to Union Ranges.
        For i = 1 To LastUR
            If StrComp(.Cells(i, cVntCol), cStrCriteria, vbTextCompare) = 0 Then
                If Not rngU1 Is Nothing Then
                    Set rngU1 = Union(rngU1, .Cells(i, 1))
                    Set rngU2 = Union(rngU2, Worksheets(cSheet2).Cells(i, 1))
                  Else
                    Set rngU1 = .Cells(i, 1)
                    Set rngU2 = Worksheets(cSheet2).Cells(i, 1)
                End If
            End If
        Next

    End With

    ' Delete rows.
    If Not rngU1 Is Nothing Then
        rngU1.EntireRow.Delete ' Hidden = True
        rngU2.EntireRow.Delete ' Hidden = True
        Set rngU2 = Nothing
        Set rngU1 = Nothing
    End If

End Sub

I think that there could be lots of formulas.我认为可能有很多公式。 So Application.Calculation = xlManual at the begining and Application.Calculation = xlCalculationAutomatic at the end should be good idea too.所以 Application.Calculation = xlManual 在开头和 Application.Calculation = xlCalculationAutomatic 在结尾也应该是个好主意。

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For r = w1.UsedRange.Rows.Count To 1 Step -1
     If Cells(r, "R") = "Old" Then
         w1.Rows(r).EntireRow.Delete
         w2.Rows(r).EntireRow.Delete
     End If
 Next r
Application.ScreenUpdating = true
Application.Calculation = xlCalculationAutomatic

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

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