Remove duplicate values

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help?

I have values in cells "E4:K4" sheet1 that change when commandbutton2 is clicked.

Cells "E5:K7" sheet1 also have values that change

If any values in cells "E5:K7" match values in cells "E4:K4" when the command button is clicked then I want to remove these values from the cells "E5:K7" and continue the same process each time the command button is clicked until there are only 7 values left in cells ""E5:K7"

Any help would be appreciated

Regards

pwill
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Do the values in
- E5:E7 need to match E4
- F5:F7 need to match F4
etc??

OR
Does any value found in any cell in E5:K7 match any value found in any cell in E4:K4
 
Last edited:
Upvote 0
Hi Yongle

any cell found in cells E5:K7 that match any value found in any cell in E4:K4. Hope that helps

pwill
 
Upvote 0
test in a copy of your worksheet

Code:
Private Sub CommandButton1_Click()
    Dim cel As Range
    For Each cel In Range("E5:K7")
        With WorksheetFunction
            If .CountA(Range("E5:K7")) > 7 And .CountIf(Range("E4:K4"), cel) >= 1 Then cel.ClearContents
        End With
    Next cel
End Sub
 
Last edited:
Upvote 0
Thank you Yongle much appreciated,
I can't try it right now as on my way home from work but will give it a try and let you know how I get on as soon as I can.

pwill
 
Upvote 0
test in a copy of your worksheet

Code:
Private Sub CommandButton1_Click()
    Dim cel As Range
    For Each cel In Range("E5:K7")
        With WorksheetFunction
            If .CountA(Range("E5:K7")) > 7 And .CountIf(Range("E4:K4"), cel) >= 1 Then cel.ClearContents
        End With
    Next cel
End Sub

Thanks Yongle, works perfect :)

I am looking to take things a step further with what I need the code for, please could you keep an eye on this thread to see if you can help with further posts.

Regards

pwill
 
Upvote 0



Hi Yongle, just wondering if you could help with this?

I have changed the range in your code from "E5:K7" to "T5:Z7". The values in "T5:Z7" are now equal to the values in "E5:K7" because the values in these cells "E5:K7" have formulas that update values when the cell values in "E4:K4" change and are needed for the process to work.

After applying your code and have the 7 values left, I highlighted them in red. I now want to continue the process but this time, only remove matching values when the 7 remaining cells that are highlighted in red match any value from "E4:K4" but without removing the 7 values highlighted in red and continue until there are only 2 values remaining with the 7 values highlighted in red.

ie

EFGHIJKLMNOPQRSTUVWXYZ
1
2StartFinish
3ABCDEFGABCDEFG
4188200360388411455488
5188266299333355399466266299466
6199277300344366400477344
7177255288322344388455255344455
8
9
EFGHIJKLMNOPQRSTUVWXYZ
1
2Continue ProcessAnd continue until only 2 values are left with the 7 highlighted in red
3ABCDEFGABCDEFG
488266366344410455480
588266299333480399466266299333399466
619988300344488455477199300344488477
7366255288322344410455255288322344455
8
9

<tbody>
</tbody>

Code:
            For Each cel In Range("T5:Z7")
                With WorksheetFunction
                    If .CountA(Range("T5:Z7")) < 1 Then Range("T5:Z7").Value = Range("E5:K7").Value
                    If .CountA(Range("T5:Z7")) > 7 And .CountIf(Range("E4:K4"), cel) >= 1 Then cel = ""
                End With
            Next cel

Hope that makes sence

pwill
 
Last edited:
Upvote 0
I have changed the range in your code from "E5:K7" to "T5:Z7". The values in "T5:Z7" are now equal to the values in "E5:K7" because the values in these cells "E5:K7" have formulas that update values when the cell values in "E4:K4" change and are needed for the process to work.
 
Upvote 0
Had a quick look and cannot grasp what is going on
Perhaps someone else will step in to help you develop this
Good luck :)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top