我正在尝试编写一个宏,让我可以根据要复制的列之前的列中的条件,将一系列数据从一个工作表复制到另一个工作表。
B列是标准列。如果此列中的任何一行中都有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
答案 0 :(得分: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