简体   繁体   English

Excel 宏在工作表中查找单元格并将整行复制到另一个工作表中

[英]Excel macro to find a cell in a sheet and copy the entire row in another sheet

I have a excel sheet with around 50k rows and i need a macro to search for a cell in that sheet and if it finds it to copy the entire row to another sheet, my problem is that the keyword may be on multiple rows so if there are like 4 cells with that keyword i need it to copy all 4 rows and paste them in another sheet我有一个大约 50k 行的 excel 工作表,我需要一个宏来搜索该工作表中的一个单元格,如果找到它将整行复制到另一个工作表,我的问题是关键字可能在多行上,所以如果有就像带有该关键字的 4 个单元格我需要它来复制所有 4 行并将它们粘贴到另一张表中

Sub saca()

Dim intPasteRow As Integer
intPasteRow = 2
Dim ceva As Range
Dim FirstAddress As String
Dim intRow As Integer

Sheets("Sheet2").Select
Columns("A:AV").Select
On Error Resume Next
Set ceva = Selection.Find(What:="m762", After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=True, SearchFormat:=True).Activate
If Not ceva Is Nothing Then
    FirstAddress = ceva.Address
    Do
        Set ceva = Selection.FindNext(ceva).Activate
    Loop While Not ceva Is Nothing And ceva.Address <> FirstAddress
End If

intRow = ActiveCell.Row
Rows(intRow & ":" & intRow).Select
Selection.Copy

Sheets("Sheet1").Select
ActiveSheet.Paste

End Sub

So far its searching for "m762" in Sheet2 but it only copies the first row with a "m762" cell instead of selecting all of them...I can't find a way to make it select all rows with "m762" in them到目前为止,它在 Sheet2 中搜索“m762”,但它只复制带有“m762”单元格的第一行,而不是选择所有单元格......我找不到一种方法让它 select 所有带有“m762”的行他们

Although not the best or quickest solution, here is a basic For Loop macro that will loop through each cell and when the criteria is found, it will copy the row in Sheet2 to the next empty row in Sheet1 by setting the values directly from Sheet2 to Sheet1 .虽然不是最好或最快的解决方案,但这里有一个基本的For Loop宏,它将遍历每个单元格,当找到条件时,它会将Sheet2中的行复制到Sheet1的下一个空行,方法是将值直接从Sheet2设置为Sheet1 It will then continue and copy each row where the criteria is found.然后它将继续并复制找到条件的每一行。

Dim cel As Range, lRow As Long

    For Each cel In ThisWorkbook.Sheets("Sheet2").Range("A2:AV" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row)
        If cel.Value = "m762" Then
            lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

            ThisWorkbook.Sheets("Sheet1").Cells(lRow + 1, 1).Resize(, 48).Value = ThisWorkbook.Sheets("Sheet2").Cells(cel.Row, 1).Resize(, 48).Value
        End If
    Next cel 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Office 2007宏-在一张工作表中查找单元格并将行复制到工作表2 - office 2007 macro - find cell in one sheet and copy row to sheet 2 将特定行(不是整个行)的特定列复制到另一张工作表-Excel Macro - Copy specific columns for particular row (not entire row) into another sheet - Excel Macro 需要宏来查找单元格值,然后将整行复制到新工作表 - Need Macro to find a cell value and then copy the entire line to a new sheet Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value 如果单元格为true,如何将整行复制到另一张工作表 - How to copy an entire row to another sheet if a cell = true Excel宏复制单元格范围并将数据粘贴到另一个工作表 - Excel Macro Copy cell range & paste data one sheet to another 宏可复制多个单元格范围并粘贴到另一张纸上的一行中 - macro to copy multiple cell ranges and paste in a row on another sheet Excel VBA - 根据数组匹配将整行复制到另一个工作表 - Excel VBA - Copy Entire Row to Another Sheet based on Array Match 查找多个字符串,复制整行并粘贴到另一个工作表中 - Find multiple strings, copy entire row and paste into another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM