在工作表之间传输数据并过滤掉excel中的重复项

时间:2015-10-04 03:00:07

标签: excel

Sheet1= master field that I am trying to copy the content from the other sheets
Sheet2= Distributor1 Column A has UPC Column B Price Column C Shipping Price
Sheet3= Distributor2 Column A has UPC Column B Price Column C Shipping Price

在Sheet 1中我想在表2和表3之间粘贴/复制所有UPC。如果有重复(比较UPC)我希望转移的两个中的较小者(较少的两个B列价格不是担心发货。)

我整天都在尝试这个,但却无法得到它。试图从两张纸上复制所有UPC到Sheet 1标记了重复项,我只是不能让它工作,所以我没有重复的产品。什么是更简单的方法和可能的解决方案?

1 个答案:

答案 0 :(得分:1)

快速提供此代码。它应该做你需要的。

Sub RemoveDuplicateUPCsAndKeepLowestPrice()

    Application.ScreenUpdating = False

    Dim Sht1 As Integer
    Dim Sht2 As Integer
    Dim Sht3 As Integer

    Sht2 = WorksheetFunction.CountA(Sheets("Sheet2").Range("A:A"))
    Sht3 = WorksheetFunction.CountA(Sheets("Sheet3").Range("A:A"))

    Sheets("Sheet1").Range("A:C").ClearContents
    Sheets("Sheet1").Range("A1:A" & Sht2 + Sht3).NumberFormat = "@"
    Sheets("Sheet1").Range("A1:C" & Sht2).Value = Sheets("Sheet2").Range("A1:C" & Sht2).Value
    Sheets("Sheet1").Range("A" & Sht2 + 1 & ":C" & (Sht3 + Sht2 - 1)).Value = Sheets("Sheet3").Range("A2:C" & Sht3).Value

    Sheets("Sheet1").Sort.SortFields.Clear
    Sheets("Sheet1").Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    Sheets("Sheet1").Sort.SortFields.Add Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With Sheets("Sheet1").Sort
        .SetRange Range("A:C")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Sheets("Sheet1").Range("A:C").RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes

    Sht1 = WorksheetFunction.CountA(Sheets("Sheet1").Range("A:A"))
    For i = 1 To Sht1
        If Sheets("Sheet1").Range("A" & i).Value = Sheets("Sheet1").Range("A" & (i + 1)).Value Then
            Sheets("Sheet1").Range("A" & i & ":C" & i).ClearContents
        End If
    Next i

    Sheets("Sheet1").Sort.SortFields.Clear
    Sheets("Sheet1").Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    Sheets("Sheet1").Sort.SortFields.Add Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With Sheets("Sheet1").Sort
        .SetRange Range("A:C")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Application.ScreenUpdating = True

End Sub