vba - Excel - Multiple selection drop down list - no duplication of selection -


i have developed on excel spreadsheet multiple items can selected in drop down list using following code:

private sub worksheet_change(byval target range)  dim rngdv range dim oldval string dim newval string if target.count > 1 goto exithandler  on error resume next set rngdv = cells.specialcells(xlcelltypeallvalidation) on error goto exithandler  if rngdv nothing goto exithandler  if intersect(target, rngdv) nothing  else   application.enableevents = false   newval = target.value   application.undo   oldval = target.value   target.value = newval     if oldval = ""        else       if newval = ""        else       target.value = oldval _         & ", " & newval        end if     end if end if   exithandler:   application.enableevents = true end sub 

but, want validate answers drop down list items can selected once. , preferably, if user selects item again, removed.

any appreciated.

try this:

private sub worksheet_change(byval target range)     const sep string = ", "     dim rngdv range     dim oldval string     dim newval string     dim arr, m, v     if target.count > 1 goto exithandler      on error resume next     set rngdv = target.specialcells(xlcelltypesamevalidation)     on error goto exithandler     if rngdv nothing exit sub      newval = target.value     if len(newval) = 0 exit sub 'user has cleared cell...      application.enableevents = false      application.undo     oldval = target.value      if oldval <> ""         arr = split(oldval, sep)         m = application.match(newval, arr, 0)         if iserror(m)             newval = oldval & sep & newval         else             arr(m - 1) = ""             newval = ""             each v in arr                 if len(v) > 0 newval = newval & iif(len(newval) > 0, sep, "") & v             next v         end if         target.value = newval     end if  exithandler:       application.enableevents = true end sub 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -