code to delete the row if the cells on specific column are unique

前端 未结 2 838
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 03:48

What I am trying to achieve is to create a vba code to completely delete the rows if the value in column C (Id) is unique. So in example below the rows 6 and 7 would be dele

2条回答
  •  一个人的身影
    2020-12-04 04:35

    I think the most efficient way would be:

    1. Initialize an empty HashSet< Integer> (or whatever generic type you want) which will represent all the unique entries of C (id), let's name it uniqueIdSet

    2. Iterate through the 2D array

    
    
        if(uniqueIdSet.contains(id)){
              //if the id was already seen before, it means it's not unique
              uniqueIdSet.remove(id);
        }
        else{
              //we haven't seen this id yet, add it to the unique set
              uniqueIdSet.add(id);
        }
    
    
    1. Iterate through the original array again and do:
    
    
        if(uniqueSet.contains(id)){
              //if the id is unique, remove it from the array.
              array.remove(currentRow);
        }
    
    

    Depending on your implementation, you might not be able to remove from the array as you iterate through it. A way around it is initializing a copy of the original array and remove the respective row from there.

提交回复
热议问题