简体   繁体   English

根据单元格值将特定数据(不是整行!)复制到另一张表

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

相关问题 根据单元格值将数据从特定列复制到另一个工作表 - Copy data from specific columns to another sheet based on a cell value 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell 在特定行上搜索值,将整列复制到另一张纸上 - Search on specific row for value, copy entire column to another sheet 将整行复制到新工作表并根据单元格值更改单元格值 - Copy entire row to a new sheet and change cell value based on cell value Excel 宏在工作表中查找单元格并将整行复制到另一个工作表中 - Excel macro to find a cell in a sheet and copy the entire row in another sheet VBA如果单元格与整个工作表的值匹配,则复制整个行 - VBA Copy entire row if cell matches a value for entire sheet 根据单元格值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on cell value 根据单元格内一个段落中的单个单词,将整行从一张纸复制到另一张纸 - Copy an entire row from one sheet to another based upon a single word, within a paragraph, inside a cell Excel VBA 自动根据单元格值复制整行“X”次并粘贴到单独的工作表中 - Excel VBA automation copy entire row “X” times based on cell value and paste in separate sheet 根据单元格值将特定行复制到另一个工作簿 - Copy specific row to another workbook based on a cell value
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM