简体   繁体   English

如果单元格为true,如何将整行复制到另一张工作表

[英]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.

相关问题 Excel 宏在工作表中查找单元格并将整行复制到另一个工作表中 - Excel macro to find a cell in a sheet and copy the entire row in another sheet 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value 使用For循环根据VBA中单元格中的文本将整行从工作表复制到另一个工作表 - Copy an entire row from a sheet to another sheet on basis of text in a cell in VBA using For loop 如何检查2列中是否有重复项并将整行复制到另一张工作表中? - How to Check for duplicates in 2 columns and copy the entire row into another sheet? 如何找到值然后将整行复制到另一张纸 - How to find value then copy entire row to another sheet 如果 CountA&lt;&gt;0 则将整行复制到另一张纸上 - Copy entire row if CountA<>0 to another sheet VBA如果单元格与整个工作表的值匹配,则复制整个行 - VBA Copy entire row if cell matches a value for entire sheet Excel:如果单元格在另一个选项卡中,如何复制整个行 - Excel: If cell exists in another tab, how to copy entire row 根据单元格内一个段落中的单个单词,将整行从一张纸复制到另一张纸 - Copy an entire row from one sheet to another based upon a single word, within a paragraph, inside a cell 如果C的单元格值与工作表名称匹配,如何将行复制到另一工作表 - How to copy row to another sheet if the cell value of C matches the sheetname
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM