[英]Copy Specific data (Not the entire row!) to another sheet based on cell value
I am trying to copy cell data from one sheet to another based on a match. 我正在尝试根据匹配将单元格数据从一张纸复制到另一张纸。
I have a workbook with 3 sheets. 我有一本3页的工作簿。 "Place", "Coke" and "HMS".
“地点”,“可乐”和“ HMS”。
On Active sheet "Place", If column C14 has the word "Coke" - I want D14-H14 copied to the sheet "Coke". 在活动工作表“位置”上,如果列C14中有单词“可乐”-我希望将D14-H14复制到工作表“可乐”中。
Similarly if C15 contains "HMS" - I want only "D15-H15" Copied to the sheet "HMS" 同样,如果C15包含“ HMS”-我只希望将“ D15-H15”复制到工作表“ HMS”
I have a macro that copies the entire row while I want specific cells copied - which is from the specific C:H. 我有一个宏可以复制整个行,而我要复制特定的单元格-这是从特定的C:H复制的。
Sub As_Of_Analysis_Sorting()
Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("Place").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Coke").Cells(Rows.Count, "A").End(xlUp).Row
lr3 = Sheets("HMS").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If Range("C" & r).Value = "Coke" Then
Rows(r).Copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
lr2 = Sheets("Coke").Cells(Rows.Count, "A").End(xlUp).Row
End If
If Range("C" & r).Value = "HMS" Then
Rows(r).Copy Destination:=Sheets("HMS").Range("A" & lr3 + 1)
lr3 = Sheets("HMS").Cells(Rows.Count, "A").End(xlUp).Row
End If
Range("A140").Select
Next r
End Sub
Hos do I achieve this? 我该如何实现?
Firstly a "good practice" suggestion: always tell which sheet the range is in: 首先是“好的做法”建议:始终告诉范围在哪个工作表中:
Range("C" & r).Value
'becomes
Range("Place!C" & r).Value
'or
sheets("Place").Range("C" & r).Value
Now the answer: the code just does what you told it to do: 现在的答案是:该代码正是按照您的指示执行的:
Rows(r).Copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
==> you copy the whole row and place it in A. That's replacing the whole row If you do NOT need format, you should use this: ==>复制整个行并将其放在A中。这将替换整个行。如果不需要格式,则应使用此格式:
range("Coke!A" & lr2 +1 & ":F" & lr2 +1 ) = range("Place!C" & r & ":H" & r).value2
if you need format (slower code) : 如果您需要格式(慢速代码):
range("Place!C" & r & ":H" & r).copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.