how change code to implementing for multiple columns instead of one

Ali M

Active Member
Joined
Oct 10, 2021
Messages
290
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello
I have this code prevent adding duplicates items for column D , but I want to change it to multiple columns ( D:H , K:M )
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 4 And Len(Target.Value) > 0 Then
    Application.EnableEvents = False
    If WorksheetFunction.CountIf(Range("D:D"), Target.Value) > 1 Then
        MsgBox Target.Value & " is a duplicate entry.  It will be removed.", vbExclamation, "Data Entry Editor" & "(INDEX(C7:C23,MATCH(Target.Value,D7:D23,0))"
        Target.ClearContents
    End If
    Application.EnableEvents = True
End If

End Sub
thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("D:M"), Target) Is Nothing Then
With Target
If Len(.Value) > 0 And (.Column < 9 Or .Column > 10) Then
Application.EnableEvents = False
If WorksheetFunction.CountIf(Range("D:M"), .Value) > 1 Then
MsgBox .Value & " is a duplicate entry, it will be removed.", vbExclamation
.ClearContents
End If
Application.EnableEvents = True
End If
End With
End If
End Sub

You cannot maintain the header of the msgbox with 'index' and match'.
 
Upvote 0
thanks .should be for each individall column I mean each column should accept the item from the first time even if there is duplicated item in another column just prevent duplicates items in the same column for each column
 
Upvote 0
should be for each individall column I mean each column should accept the item from the first time even if there is duplicated item in another column
That was not clear in your first post, because there you wrote:
I want to change it to multiple columns ( D:H , K:M )

For each individual column, you can use:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("D:M"), Target) Is Nothing Then
Dim myrange As Range
With Target
If Len(.Value) > 0 And (.Column < 9 Or .Column > 10) Then
Set myrange = Columns(.Column)
Application.EnableEvents = False
If WorksheetFunction.CountIf(myrange, .Value) > 1 Then
MsgBox .Value & " is a duplicate entry, it will be removed.", vbExclamation
.ClearContents
End If
Application.EnableEvents = True
End If
End With
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,419
Messages
6,124,796
Members
449,189
Latest member
kristinh

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