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