blank out duplicate cells based on value that is changes in another column

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
hi all

I'm locking for an excel VBA that would blank out cells in Col D (NOT delete entire row) when duplicate data is found but the trick is that it needs to be based on when there is a change of data in column C for ex.

Column CColumn D
ABC123
ABC123
ABC123
DEF456
DEF456

So my goal here is to just have the first instances show 123 and the rest blank ETC..

thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
VBA Code:
Sub EraseDupeValuesCol2()
    Dim c As Range
    Dim r As Range
   
    Set r = Range(Cells(1, 3), Cells(Cells(Rows.Count, 3).End(xlUp).Row, 3))
   
    For Each c In r
        If Cells(c.Row + 1, c.Column).Value = c.Value Then
            Cells(c.Row + 1, c.Column + 1).ClearContents
        End If
    Next
   
End Sub

Solutions 20210722.001.xlsm
CD
1ABC123
2ABC
3ABC
4DEF456
5DEF
6GHI789
7GHI
8GHI
9GHI
blank out
 
Upvote 0
VBA Code:
Sub EraseDupeValuesCol2()
    Dim c As Range
    Dim r As Range
  
    Set r = Range(Cells(1, 3), Cells(Cells(Rows.Count, 3).End(xlUp).Row, 3))
  
    For Each c In r
        If Cells(c.Row + 1, c.Column).Value = c.Value Then
            Cells(c.Row + 1, c.Column + 1).ClearContents
        End If
    Next
  
End Sub

Solutions 20210722.001.xlsm
CD
1ABC123
2ABC
3ABC
4DEF456
5DEF
6GHI789
7GHI
8GHI
9GHI
blank out
Thanks can this be modifed to have column letters like a or c or d the reason being the criteria changes sometimes

thanks
 
Upvote 0
Are you going to select the range that you want to clear values from? If so, will they always be two columns AND will you want to clear the 2nd column based on the changes in the first?
 
Upvote 0
Are you going to select the range that you want to clear values from? If so, will they always be two columns AND will you want to clear the 2nd column based on the changes in the first?
hi

no i will not be selecting actually my real column that i want modified is column BF and it will be based off column AB
 
Upvote 0
So you want to be able to modify the code more easily to adjust to different source and target columns?

VBA Code:
Sub EraseDupeValuesCol2()
    Dim c As Range
    Dim r As Range
    Dim srcCol As String
    Dim trgCol As String
    Dim co As Long
    
    
    srcCol = "AB"
    trgCol = "BF"
    co = Range(trgCol & ":" & trgCol).Column - Range(srcCol & ":" & srcCol).Column
    
    Set r = Range(srcCol & "1:" & srcCol & Cells(Rows.Count, srcCol).End(xlUp).Row)
    For Each c In r
        If Range(c.Address).Offset(1).Value = c.Value Then
            Range(c.Address).Offset(1, co).ClearContents
        End If
    Next
    
End Sub
 
Upvote 0
So you want to be able to modify the code more easily to adjust to different source and target columns?

VBA Code:
Sub EraseDupeValuesCol2()
    Dim c As Range
    Dim r As Range
    Dim srcCol As String
    Dim trgCol As String
    Dim co As Long
   
   
    srcCol = "AB"
    trgCol = "BF"
    co = Range(trgCol & ":" & trgCol).Column - Range(srcCol & ":" & srcCol).Column
   
    Set r = Range(srcCol & "1:" & srcCol & Cells(Rows.Count, srcCol).End(xlUp).Row)
    For Each c In r
        If Range(c.Address).Offset(1).Value = c.Value Then
            Range(c.Address).Offset(1, co).ClearContents
        End If
    Next
   
End Sub
Thank you very much that worked perfectly
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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