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