我有数据表,我想在其他表格中获取数据但有条件。这就是我的源数据:
-------------------------------------------------
| Cell A | Cell B | Cell C | Cell D|Cell E |
|------------------------------------------------|
| Sku |Order_ID|Customer_ID | Price |Status |
|------------------------------------------------|
| TW22 | 123 | 1 |221 | D |
|------------------------------------------------|
| TS44 | 124 | 2 |221 | D |
|------------------------------------------------|
| Ts11-ab | 33 | 3 |211 | D |
|------------------------------------------------|
| Ts11 | 231 | 4 |231 | D |
|------------------------------------------------|
| Ts11-ab | 33 | 3 |211 | R |
|------------------------------------------------|
| Ts11 | 231 | 4 |231 | R |
|------------------------------------------------|
如果有两行具有相同的订单ID,我想删除第一行(状态为D)并且只保留第二行(状态R)。上述数据的所需输出为:
--------------------------------------------------
| Cell A | Cell B | Cell C | Cell D|Cell E |
|------------------------------------------------|
| Sku |Order_ID|Customer_ID | Price |Status |
|------------------------------------------------|
| TW22 | 123 | 1 |221 | D |
|------------------------------------------------|
| TS44 | 124 | 2 |221 | D |
|------------------------------------------------|
| Ts11-ab | 33 | 3 |211 | R |
|------------------------------------------------|
| Ts11 | 231 | 4 |231 | R |
|------------------------------------------------|
这是我尝试过的公式:
=IFERROR(INDEX(Sheet1!E$2:E$5,(MATCH($B2,Sheet1!B$2:B$5,0))),-1)
答案 0 :(得分:0)
从aditya得到答案..谢谢这里是他提供的
Sub copy_sheet()
Dim raw_data, new_data As Worksheet
Set raw_data = ThisWorkbook.Sheets("raw_data")
Set new_data = ThisWorkbook.Sheets("new_data")
raw_data.Activate
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Copy
new_data.Activate
Range("A1").PasteSpecial xlPasteValues
Range("A1").Sort key1:=Range("E1"), order1:=xlDescending, Header:=xlYes
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
Range("A1").Sort key1:=Range("E1"), order1:=xlAscending, Header:=xlYes
Range("A1").Activate
End Sub
答案 1 :(得分:0)
我不知道你已经尝试了什么,但据我所知,带有“R”的行总是在带有“D”的行之后,对吗?你可以按相反的顺序浏览你的表并记住所有带有R的行。在伪代码中如下:
size = yourTable.Rows.Count
Dim col As New Collection
For column = size To 1 Step -1
If StatusOfCurrentRow = "R" Then
Copy Row to Sheet2
col.Add Value of Sku
Else
If SkuOfCurrentRow not in col
Copy Row to Sheet2
End If
End If
Next