【问题标题】:VBA code to copy and paste specifc words in different cells to another worksheetVBA代码将不同单元格中的特定单词复制并粘贴到另一个工作表
【发布时间】:2019-02-26 14:49:30
【问题描述】:

我有一个代码可以复制并粘贴一整行到名为“原始数据”的工作表中。如果范围 $D$1:D 中的单元格的值为“Thomas Xiong”,那么它将将该值下的所有内容的整行粘贴到另一个名为“WIP”的工作表中。

我想要做的是能够创建一个能够找到多个单词的代码。例如,“Thomas Xiong”和“Assigned”一词,并且能够将整行从工作表“Raw Data”复制并粘贴到另一个工作表中。

还有我现在拥有的代码,它会复制并粘贴整行,但另一个工作表中的每个单元格行之间都有空格。

我现在的代码:

Sub Test()

Dim Cell As Range

With Sheets("Raw Data")
' loop column C untill last cell with value (not entire column)
For Each Cell In .Range("D1:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
    If Cell.Value = "Thomas Xiong" Then
         ' Copy>>Paste in 1-line (no need to use Select)
        .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(Cell.Row)
        '.Range("C1:C", "A", "B", "D", "F" & Cell.Row).copy
    End If
  Next Cell
For Each Cell In .Range("C1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
    If Cell.Value = "Assigned" Then
         ' Copy>>Paste in 1-line (no need to use Select)
        .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(Cell.Row)
        '.Range("C1:C", "A", "B", "D", "F" & Cell.Row).copy
    End If
Next Cell
End With

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    问题是您正在使用您在目标工作表中复制的单元格行。你想使用一个单独的计数器,每次你在给定的行上粘贴一些东西时都会增加:

    Sub Test()
    
    Dim Cell As Range
    Dim myRow as long
    
    myRow = 2
    With Sheets("Raw Data")
        ' loop column C untill last cell with value (not entire column)
        For Each Cell In .Range("D1:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            If Cell.Value = "Thomas Xiong" Then
                 ' Copy>>Paste in 1-line (no need to use Select)
                .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(myRow)
                myRow = myRow + 1
            End If
        Next Cell
        For Each Cell In .Range("C1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
            If Cell.Value = "Assigned" Then
                ' Copy>>Paste in 1-line (no need to use Select)
                .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(myRow)
                myRow = myRow + 1
            End If
        Next Cell
    End With
    
    End Sub
    

    不清楚(至少对我而言)是如果您只想找到 D 列中的值为“Thomas Xiong”的行并且C 列中的值为“已分配”,在这种情况下,您想要这样的东西:

    Sub Test()
    
    Dim Cell As Range
    Dim myRow as long
    
    myRow = 2
    With Sheets("Raw Data")
        For Each Cell In .Range("C1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
            If Cell.Value = "Assigned" and Cell.Offset(0,1).Value = "Thomas Xiong" Then
                ' Copy>>Paste in 1-line (no need to use Select)
                .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(myRow)
                myRow = myRow + 1
            End If
        Next Cell
    End With
    
    End Sub
    

    要遍历名称列表(我假设在名为“myNames”的工作表中位于A1:A10 范围内),这样的方法应该可以:

    Sub Test()
    
    Dim Cell as Range
    Dim NameCell as Range
    Dim myRow as Long
    
    myRow = 2
    With Sheets("Raw Data")
        For each NameCell in Worksheet("myNames").Range("A1:A10)
            For Each Cell In .Range("C1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
                If Cell.Value = "Assigned" and Cell.Offset(0,1).Value = NameCell.Value Then
                    .Rows(Cell.Row).copy Destination:=Sheets("WIP").Rows(myRow)
                    myRow = myRow + 1
                    Exit For
                End If
            Next Cell
        Next NameCell
    End With
    
    End Sub
    

    【讨论】:

    • 感谢您提供代码,但如果我想在“原始数据”电子表格中搜索多个名称或多个单词并复制并粘贴到选项卡中,我该如何添加该代码?例如,每个选项卡下都有不同的名称。
    • 如果您有一个包含名称列表的表,请遍历它,将名称分配给一个变量并使用以下内容进行测试:If Cell.Value = "Assigned" and Cell.Offset(0,1).Value = <NameVariable>
    • 所以我用您上面粘贴的代码对此进行了测试,但是当数据被复制并粘贴到工作表中时,它不会进入下一个空白行?是因为 myRow = myRow +1 吗?
    • 不确定你的意思,这可能是一个新问题
    • 您上面所说的代码有效,但是当我尝试为不同的值运行它并将其复制并粘贴到同一个工作表中时,它会删除代码运行的先前数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多