简体   繁体   English

VBA代码可将特定单元格中的特定单词复制并粘贴到另一个工作表中

[英]VBA code to copy and paste specifc words in different cells to another worksheet

I have a code that will copy and paste a whole row in worksheet called "Raw Data". 我有一个代码,它将在工作表中复制并粘贴整行,称为“原始数据”。 If cells in Range $D$1:D have a value of "Thomas Xiong", then it will paste the whole row of everything under that value to another worksheet called "WIP". 如果范围$ D $ 1:D中的单元格的值为“ Thomas Xiong”,则它将把该值下的所有内容的整个行粘贴到另一个名为“ WIP”的工作表中。

What I am trying to do is be able to create a code that will be able to find multiple words. 我正在尝试做的是能够创建一个能够找到多个单词的代码。 For example, "Thomas Xiong" and the word "Assigned" and be able to copy and paste that whole line from the worksheet "Raw Data" into another worksheet. 例如,“ Thomas Xiong”和单词“ Assigned”,并能够将整个行从工作表“原始数据”复制并粘贴到另一个工作表中。

Also with the code I have now, it will copy and paste the whole rows but there are spaces in between each cell row in the other worksheet. 同样,使用我现在拥有的代码,它将复制并粘贴整个行,但是另一个工作表中的每个单元格行之间都有空格。

The code I have now: 我现在拥有的代码:

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

The problem is you are using the row of the cells you are copying in your destination sheet. 问题是您正在使用目标表中要复制的单元格行。 You want to use a separate counter that you increment every time you paste something on e given row: 您想使用一个单独的计数器,该计数器每次在给定行上粘贴某些内容时都会递增:

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

What's not clear (to me at least) is if you want to only find rows where the value in column D is "Thomas Xiong" and the value in column C is "Assigned", in which case you want to have something like this: 尚不清楚(至少对我而言)是,如果您只想查找D列中的值为“ Thomas Xiong” C列中的值为“ Assigned”的行,在这种情况下,您希望具有以下内容:

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

To loop through a list of names (which I will assume to be in range A1:A10 in a worksheet called "myNames") something like this should work: 要遍历名称列表(在工作表“ 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

暂无
暂无

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

相关问题 如果条件满足,我如何使用 VBA 代码复制和粘贴特定单元格到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met to different areas of another worksheet 如果在两个或多个工作表中满足条件,我如何使用 VBA 代码将特定单元格复制和粘贴到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met in two or more worksheets to different areas of another worksheet 将单元格粘贴到VBA中的另一个工作表 - Paste cells to another worksheet in VBA Excel vba将来自不同工作表的单元格复制并粘贴到新工作表 - Excel vba to copy and paste a cells from different worksheets to new worksheet VBA代码可使用文件名将一个工作表中的多个单元格粘贴到另一个工作表中 - VBA Code to paste multiple cells from one worksheet into another with the filenames 从一行复制特定的单元格,然后粘贴到另一工作表的不同单元格中 - Copy specific cells from one row and paste into different cells on another worksheet VBA代码可从一个工作表复制数据并将其粘贴到另一工作表的最后一行下方 - VBA code to copy data from one worksheet & paste below last row of another worksheet 如何根据VBA中的预定义索引从另一个工作表复制和粘贴一系列单元格 - How to copy and paste a range of cells from another worksheet depending on predefined indices in VBA 在不同工作表的列中查找匹配项,然后将特定单元格复制粘贴到 VBA 中的匹配行 - Find match within a column in a different worksheet, then copy paste specific cells to matched row in VBA 将字典存储在另一个工作表中的单元格中的VBA粘贴单元格 - VBA Paste Cells Stored in Dictionary to Cells in Another Worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM