VBA - Restrict Code to One Column for Multi-choice Dropdown

welshraz

New Member
Joined
Apr 29, 2016
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have this code which works, but I would like to restrict it to just one column - is this possible? It would be massively helpful to prevent users from doing them same in additional columns. Thanks!

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Destination As Range)
Dim rngDropdown As Range
Dim oldValue As String
Dim newValue As String
Dim DelimiterType As String
DelimiterType = ", "
 
If Destination.Count > 1 Then Exit Sub
 
On Error Resume Next
Set rngDropdown = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitError
 
If rngDropdown Is Nothing Then GoTo exitError
 
If Intersect(Destination, rngDropdown) Is Nothing Then
   'do nothing
Else
  Application.EnableEvents = False
  newValue = Destination.Value
  Application.Undo
  oldValue = Destination.Value
  Destination.Value = newValue
    If oldValue <> "" Then
    If newValue <> "" Then
        If oldValue = newValue Or _
            InStr(1, oldValue, DelimiterType & newValue) Or _
            InStr(1, oldValue, newValue & Replace(DelimiterType, " ", "")) Then
            Destination.Value = oldValue
                Else
            Destination.Value = oldValue & DelimiterType & newValue
        End If
    End If
    End If
End If
 
exitError:
  Application.EnableEvents = True
End Sub
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi @welshraz.
Edit this line:
VBA Code:
If Destination.Count > 1 Then Exit Sub
with:
Code:
If Destination.Count > 1 Or Destination.Column <> 1 Then Exit Sub
Now the macro will only run when there is a change in column A (or another column if you specify a different number in Destination.Column). Replace 1 with the number of the desired column to configure for the desired column.
 
Upvote 0
Hi @welshraz.
Edit this line:
VBA Code:
If Destination.Count > 1 Then Exit Sub
with:
Code:
If Destination.Count > 1 Or Destination.Column <> 1 Then Exit Sub
Now the macro will only run when there is a change in column A (or another column if you specify a different number in Destination.Column). Replace 1 with the number of the desired column to configure for the desired column.
Thanks. This works perfectly, except now the powers that be want to restrict it to two columns....can this be done, or do I need to go back to my original solution?
 
Upvote 0

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

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