VBA inquiry to remove duplicates

rn119

New Member
Joined
Feb 27, 2013
Messages
49
I have a column that has duplicate values that I would like to remove. If I reference the column (Column N in this case) I can get this to work with the following syntax.

Private Sub CommandButton1_Click()


Dim firstvalue As String
Dim lastvalue As String
Dim arraystr() As String
Dim x As Long
Dim k As Long
Dim cell As Range
Dim rw As Long


Application.ScreenUpdating = False

For Each cell In Sheets("Sheet1").Range("N1:N" & Cells(Rows.Count, 1).End(xlUp).Row)
Erase arraystr ' erase array
lastvalue = "" ' erase final value"
firstvalue = cell.Value
On Error Resume Next

arraystr = Split(firstvalue, ",")


For rw = 0 To UBound(arraystr)

For k = rw + 1 To UBound(arraystr)
If Trim(arraystr(k)) = Trim(arraystr(rw)) Then
arraystr(k) = ""
End If
Next k
Next rw


For x = 0 To UBound(arraystr)
If arraystr(x) <> "" Then

lastvalue = lastvalue & Trim(arraystr(x)) & ", "
End If
Next x

lastvalue = Trim(lastvalue)
lastvalue = Left(lastvalue, Len(lastvalue) - 1)


cell.Offset(0, 0).Value = lastvalue
Next cell


Columns("N:N").Select
Selection.EntireColumn.AutoFit


Application.ScreenUpdating = True
End Sub

I prefer to do it by the column name (let's call it 'Invoice Description') since the columns can shift over time. Any help?

 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try
Code:
Private Sub CommandButton1_Click()

Dim Clmn As Long
Dim firstvalue As String
Dim lastvalue As String
Dim arraystr() As String
Dim x As Long
Dim k As Long
Dim cell As Range
Dim rw As Long


Application.ScreenUpdating = False
Clmn = Range("1:1").find("Invoice Description", , , xlWhole, , , False, , False).Column
For Each cell In Sheets("Sheet1").Range(Cells(1, Clmn), Cells(Rows.Count, Clmn).End(xlUp))
Erase arraystr ' erase array
lastvalue = "" ' erase final value"
firstvalue = cell.Value
On Error Resume Next

arraystr = Split(firstvalue, ",")


For rw = 0 To UBound(arraystr)

For k = rw + 1 To UBound(arraystr)
If Trim(arraystr(k)) = Trim(arraystr(rw)) Then
arraystr(k) = ""
End If
Next k
Next rw


For x = 0 To UBound(arraystr)
If arraystr(x) <> "" Then

lastvalue = lastvalue & Trim(arraystr(x)) & ", "
End If
Next x

lastvalue = Trim(lastvalue)
lastvalue = Left(lastvalue, Len(lastvalue) - 1)


cell.Offset(0, 0).Value = lastvalue
Next cell


Columns(Clmn).Select
Selection.EntireColumn.AutoFit


Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,981
Members
448,538
Latest member
alex78

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