我有2张纸,“初始”和“报告1”。当列“ H”中的单元格为“进行中”时,我尝试将特定行从“初始”复制到“ Report1”。
我在excel中具有按钮功能,但无法锻炼如何复制和粘贴行并移至下一个单元格。
此外,D列是公式,需要特殊粘贴才能复制。
我已附上我尝试过的当前代码,但它出错。任何帮助将不胜感激。
Sub GenRep1_Click()
Dim Inti As Worksheet
Dim rep1 As Worksheet
Set Inti = ThisWorkbook.Worksheets("Inital")
Set rep1 = ThisWorkbook.Worksheets("Report1")
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("Inti").Range("H5:H9999")
For Each cell In rngA
If cell.Value = "On going" Then
cell.EntireRow.Copy
Sheets("Inti").Range("").End(xlDown).Select
ActiveSheet.Paste
End If
Next cell
End Sub
我希望将“正在进行中”的“ H”列中的所有行复制到“ Report1”。
答案 0 :(得分:1)
我认为这可以满足您的需求。您可能想提高循环范围,以防万一,例如100个单元格的数据。
比循环更快的方法是自动过滤。
Sub GenRep1_Click()
Dim Inti As Worksheet
Dim rep1 As Worksheet
Set Inti = ThisWorkbook.Worksheets("Inital") 'check name - typo?
Set rep1 = ThisWorkbook.Worksheets("Report1")
Dim rngA As Range
Dim cell As Range
Set rngA = Inti.Range("H5:H9999") 'already defined worksheet so just use variable
'Set rngA = Inti.Range("H5",inti.range("H" & rows.count).end(xlup)) 'would be more efficient
For Each cell In rngA
If cell.Value = "On going" Then
cell.EntireRow.Copy
repl.Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial xlValues 'copy to the other sheet
End If
Next cell
End Sub