Sub Test()
Dim Cell As Range
With Sheets(1)
' loop column H untill last cell with value (not entire column)
For Each Cell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
If Cell.Value = "0" Then
' Copy>>Paste in 1-line (no need to use Select)
.Rows(Cell.Row).Copy Destination:=Sheets(4).Rows(Cell.Row)
End If
Next Cell
End With
End Sub
最佳答案
尝试
Sub Test()
Dim Cell As Range
With Sheets(1)
' loop column H untill last cell with value (not entire column)
For Each Cell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
If Cell.Value = "0" Then
' Copy>>Paste in 1-line (no need to use Select)
Dim targetRow As Long
targetRow = Cell.Row
.Range("A" & targetRow & ":H" & targetRow).Copy Destination:=Sheets(4).Rows(Cell.Row)
End If
Next Cell
End With
End Sub
但因为这会产生奇怪的重复粘贴,你真的想要吗?注意我用 currentCell 替换了变量单元格以避免混淆
Option Explicit
Sub Test()
Dim currentCell As Range
With Sheets(1)
For Each currentCell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
If currentCell.Value = "0" Then
Dim targetRow As Long
targetRow = currentCell.Row
.Range("A" & targetRow & ":H" & targetRow).Copy Destination:=Sheets(4).Cells(currentCell.Row, 1)
End If
Next currentCell
End With
End Sub
和:正如@DisplayName 所指出的,您在评论中提到了 H 列。如果你打算循环这个然后改变:
For Each currentCell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
为了 For Each currentCell In .Range("H1:H" & .Cells(.Rows.Count, "H").End(xlUp).Row)
关于vba - 该程序选择整行,如何将选择限制为A到M,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49334481/