[英]How to copy an entire row to another sheet if a cell = true
I have 2 sheets, 'Initial' & 'Report1'. 我有2张纸,“初始”和“报告1”。 I'm trying to copy specific rows from 'Inital' to 'Report1' when the cell in column 'H' is = "On going". 当列“ H”中的单元格为“进行中”时,我尝试将特定行从“初始”复制到“ Report1”。 I have the function as a button in excel but cant workout how to copy and paste the line and move onto the next cell. 我在excel中具有按钮功能,但是无法锻炼如何复制和粘贴行并移动到下一个单元格。
Also, Column D is formula and needs to be pasted special to copy over. 此外,D列是公式,需要特殊粘贴才能复制。
I have attached the current code I have tried but it errors. 我已经附上了我尝试过的当前代码,但是它出错了。 Any help would be greatly appreciated. 任何帮助将不胜感激。
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
I expect the all rows in column 'H' that = "On Going" to be copied to "Report1". 我希望列“ H”中=“正在进行”的所有行都将复制到“ Report1”。
I think this does what you want. 我认为这可以满足您的需求。 You might want to improve the range you're looping through in case you only have, eg 100 cells of data. 如果您只有100个数据单元,则可能需要提高循环范围。
A quicker approach than looping would be AutoFilter. 比循环更快的方法是自动筛选。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.