get a number of unique values without separating values that belong to the same block of values

后端 未结 6 442
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 15:06

I\'m OK with either a PL/SQL solution or an Access VBA/Excel VBA (though Access VBA is preferred over Excel VBA) one. so, PL/SQL is the first choice, Access VBA is second a

6条回答
  •  时光说笑
    2020-12-19 15:29

    Not sure about your requirement but this is the best I have understood your question. First the code is sorting the data on Fax and then extracting the IDs where Fax is appearing for the the first time, even after that because of the data, there are duplicates IDs, so again sorting and removing duplicates is being done.

    Sub Unique_fax()
    

    Finding the last row so that loop can run that many times

    lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    

    Copying the data to new rows so that your original data remains intact

    For i = 1 To lastrow
    
    Worksheets("Sheet1").Cells(i, 5).Value = Trim(Worksheets("Sheet1").Cells(i, 1))
    Worksheets("Sheet1").Cells(i, 6).Value = Trim(Worksheets("Sheet1").Cells(i, 2))
    Worksheets("Sheet1").Cells(i, 7).Value = Trim(Worksheets("Sheet1").Cells(i, 3))
    
    Next
    

    Sorting the data based on Fax

    Range("E1:G" & lastrow).Select
        Selection.Sort Key1:=Range("G1"), Order1:=xlAscending, _
                       Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    

    Copying the IDs where the Fax is different to a new row

    x = 1
    For i = 1 To lastrow
    If Cells(i, 7) <> Cells(i + 1, 7) Then
    Cells(x, 9) = Cells(i, 6)
    x = x + 1
    End If
    Next
    

    Sorting the list of IDs and removing duplicates

    lastrowUnq = Worksheets("Sheet1").Cells(Rows.Count, 9).End(xlUp).Row
    
    Range("I1:I" & lastrowUnq).Select
        Selection.Sort Key1:=Range("I1"), Order1:=xlAscending, _
                       Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    y = 1
    For j = 1 To lastrow
    If Cells(j, 9) <> Cells(j + 1, 9) Then
    Cells(y, 11) = Cells(j, 9)
    y = y + 1
    End If
    Next
    
    End Sub
    

    Column - A,B,C is your original Data. Column - E,F,G is the data sorted on Fax. Column - I contains the list of IDs where Fax was unique. Column - K contains the final list of IDs(as required).

    enter image description here

提交回复
热议问题