[英]Copy row to another sheet based on value in a cell
I need to copy a row of data onto another sheet based on a value in that row. 我需要根据该行中的值将数据行复制到另一张纸上。 The value is 0 and will always be found in Column J of the origin sheet.
该值为0,将始终在原始表的J列中找到。 I want columns A - N copied to the second sheet.
我希望将A-N列复制到第二张工作表。 I found this script which copies the entire row.
我发现此脚本复制了整行。 The problem is I need to preserve any data that was previously entered onto the second (destination) sheet in columns O - AZ.
问题是我需要保留以前在O-AZ列的第二个(目标)表中输入的所有数据。 Unfortunately, the script below pastes the entire row to the second (destination) sheet and any data that was entered in columns O - AZ is lost.
不幸的是,下面的脚本将整个行粘贴到第二个(目标)工作表上,并且在O-AZ列中输入的所有数据都会丢失。
Sub MyMacro()
Dim i As Long, iMatches As Long
Dim aTokens() As String: aTokens = Split("0", ",")
For Each Cell In Sheets("AMI").Range("J:J")
If Len(Cell.Value) <> 0 Then
For i = 0 To UBound(aTokens)
If InStr(1, Cell.Value, aTokens(i), vbTextCompare) Then
iMatches = (iMatches + 1)
Sheets("AMI").Rows(Cell.Row).Copy Sheets("AMI Fallout").Rows(iMatches + 1)
End If
Next
End If
Next
End Sub
You are copying a complete row with code like: 您正在复制包含以下代码的完整行:
Sub dural()
Sheets("Sheet1").Rows(11).Copy Sheets("Sheet2").Rows(17)
End Sub
To copy only part of the row, use something like: 要仅复制行的一部分 ,请使用类似以下内容的内容:
Sub dural2()
Set r1 = Intersect(Sheets("Sheet1").Rows(11), Sheets("Sheet1").Columns("A:N"))
Set r2 = Intersect(Sheets("Sheet2").Rows(13), Sheets("Sheet2").Columns("A:N"))
r1.Copy r2
End Sub
To answer your specific question, this code will only copy columns A:n of the specified row to the AMI Fallout worksheet. 为了回答您的特定问题,此代码只会将指定行的A:n列复制到AMI Fallout工作表中。
Sheets("AMI").Cells(Cell.Row, 1).Resize(1, 14).Copy Sheets("AMI Fallout").Cells(iMatches + 1, 1)
I'm concerned about how you are determining a positive criteria for the row transfer. 我担心您如何确定行传输的积极标准。 It looks like you are trying to
Split
a 0 on a comma (which doesn't exist) then loop through a single value array and check for partial matches on a 0 . 看起来您正在尝试在逗号(不存在)上
Split
0 ,然后循环遍历单个值数组并检查0上的部分匹配项。 The partial matches produced by InStr
are the most disconcerting. InStr
产生的部分匹配最令人不安。
使用这样的东西
Sheet1.Rows(cell.row) = sheet2.Rows(Cell.row).Value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.