VBA: delete duplicates in a range except for the first one

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have a situation like this:

https://imgur.com/a/1MTsnEQ

I need, for the range B2:R2, to delete all the value "closed" except for the first.


My attempt is not concluded successfully (I can count the occurrences, but not delete).

Code:
Dim rng As Range

Dim q As Integer
Set rng = Worksheets("Foglio1").Range("B2:R2")
q = Application.WorksheetFunction.CountIf(rng, "*closed*")
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this out.... tested and works

Code:
Sub ByeClosed()
Dim r As Range
Dim counter As Integer
counter = 0
For Each r In Range("B2:R2")
        If r.Value = "Closed" Then
            counter = counter + 1
                If counter > 1 Then
                    r.Value = ""
                End If
        End If
Next
End Sub
 
Upvote 0
Well, it works perfectly.

Now, I'm trying to shift to left the populated cells.
The problem is the following: on the right of column R, other columns are populated and these have to stay where they are (not shifted to left).

How can I manage this?

(this is the uncorrect code: it shifts to left, but also shifts columns on the right of column R).
Code:
Dim rng2 As Range
    Set rng2 = Range("B2:R2").SpecialCells(xlCellTypeBlanks)
        rng2.Rows.Delete Shift:=xlToLeft
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,256
Members
449,149
Latest member
mwdbActuary

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