0

我正在尝试编写一个宏,它可以让我根据要复制的列之前的列中的条件将一系列数据从一张表复制到另一张表。

B 列是标准列。如果此列中的任何行中有 1,则将从有 1 的那一行复制 C 到 AN 列,并将其粘贴到从该表顶部开始的另一表中。

我有以下代码。它找到满足条件的第一行并将该行复制到第二张表,但是代码不会循环查找满足条件的其他行。如何调整代码以循环和复制满足条件的每个实例?

Sub testIt()

Dim i As Integer

    Application.ScreenUpdating = False

    Sheets("DataDump").Activate

    For i = 2 To Range("B2").End(xlDown).Row()
        If Range("B" & i).Value = 1 Then
            Range("C" & i, "AN" & i).Copy
            Sheets("PriceData").Activate
            ActiveSheet.Range("B2", "AM2").Select
            ActiveSheet.Paste
        End If
    Next i

    Application.ScreenUpdating = True

End Sub
4

1 回答 1

0
Sub testIt()

Dim i As Long, shtSrc As Worksheet, rngDest As Range

    Application.ScreenUpdating = False

    Set shtSrc = Sheets("DataDump")
    Set rngDest = Sheets("PriceData").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)

    For i = 2 To shtSrc.Range("B2").End(xlDown).Row
        If shtSrc.Range("B" & i).Value = 1 Then
            shtSrc.Range("C" & i & ":AN" & i).Copy rngDest
            Set rngDest = rngDest.Offset(1, 0)
        End If
    Next i

    Application.ScreenUpdating = True

End Sub
于 2013-06-25T17:25:22.367 回答