【问题标题】:How to delete rows that have just been cut & pasted into another sheet如何删除刚刚剪切并粘贴到另一个工作表中的行
【发布时间】:2020-01-14 15:03:30
【问题描述】:

我的代码将行剪切并粘贴到第二张表中,其中有一个包含“是”的单元格。

如何从源工作表中删除剩余的空白行?

Option Explicit

Sub Copy_n_Paste()
    On Error Resume Next

    Dim srchtrm As String
    Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim c As Range
    Dim i As Integer
    Dim Today As Date

    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    Set shtSrc = Sheets("DOCS")
    Set shtDest = Sheets("ARCHIVE")
    destRow = Worksheets("ARCHIVE").UsedRange.Rows.Count

    Set rng = Application.Intersect(shtSrc.Range("R2:R1000"), shtSrc.UsedRange)

    For Each c In rng.Cells
        If c.Value = "Yes" Then

            c.EntireRow.Cut shtDest.Cells(destRow, 1)

            destRow = destRow + 1

        End If
    Next

    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
    End With

    Application.CutCopyMode = False
    Sheets("DOCS").Range("A1").Select

End Sub

【问题讨论】:

  • 旁注 - 查看this question 以更好地找到最后一行并避免使用UsedRange

标签: excel vba


【解决方案1】:

这将删除整行:

c.EntireRow.Delete

不过,在循环中要小心它。这里有更彻底的讨论:Delete a row in Excel VBA

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2023-04-10
    相关资源
    最近更新 更多