clear contents if cells are equal except first

ss6857

New Member
Joined
Jan 17, 2011
Messages
27
I am try to format a report so it is simpler to read. Right now I export information from Access. The product names are repetitive and I want to use them as titles, so I want to keep the first title then delete all the similar ones underneath it. Right now my data looks similar to this when exported:

Code:
Apples
Apples
Apples
Apples
Oranges
Oranges
Oranges
Kiwi
Kiwi
Kiwi
Kiwi
Kiwi
Grape
Grape

My goal is for it to look like this:
Code:
Apples
 
 
 
Oranges
 
 
Kiwi
 
 
 
 
Grape

But with my current code I'm getting this:
Code:
Apples
 
Apples
 
Oranges
 
Oranges
Kiwi
 
Kiwi
 
Kiwi
Grape

It skips every other one unless the title changes. I'm so close! Here is my code:
Code:
Do Until Cells(myRows, 1) = ""
    If Cells(myRows, 1) = Cells(myRows - 1, 1) Then
        Cells(myRows, 1).ClearContents
        myRows = myRows + 1
    End If
    myRows = myRows + 1
Loop

Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If your fruit is in column A, put conditional formatting on A2 with the formula
=(COUNTIF($A$1:$A2,A2)>1) to turn the font color white. This can be copied to the rest of column A.
 
Upvote 0
I don't think so. Because when I advance filter > unique values only what it does is get all of the rows underneath it, which I need because it's contains information about the product. Unless I forgot how to use Advance filter correctly.
 
Upvote 0
If you want the cells truly cleared instead of just disguised, you could work from the bottom up.

Code:
Dim rowNum as Long

With Range("A:A")
    For i = .Cells(.Rows.Count, 1).End(xlup).Row To 2 Step -1
        If .Cells(i, 1) = .Cells(i-1, 1) Then .Cells(i,1).ClearContents
    Next I
End With
 
Upvote 0
Try:
Code:
Sub del()
For i = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
    With WorksheetFunction
        If .CountIf(Range("B:B"), Range("B" & i)) > 1 Then
            Range("B" & i).ClearContents
        End If
    End With
Next
End Sub
 
Upvote 0
Wow! Thanks everybody for the replies! I used mikerickson's buttom up approach and it worked wonderfully! Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

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