简体   繁体   English

根据单元格中的值将行复制到另一张工作表

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

相关问题 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value 尝试根据 A 列中的值将一行及其之前的行复制到另一张工作表 - trying to copy a row and the row before it to another sheet based on a value in column A 如果C的单元格值与工作表名称匹配,如何将行复制到另一工作表 - How to copy row to another sheet if the cell value of C matches the sheetname 根据单个单元格值将项目复制/粘贴到另一个工作表上 - Copy/Paste Item onto another sheet based on a single cell value 根据单元格值将数据从特定列复制到另一个工作表 - Copy data from specific columns to another sheet based on a cell value 根据单元格值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on cell value 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 将整行复制到新工作表并根据单元格值更改单元格值 - Copy entire row to a new sheet and change cell value based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM