Hide used items in dropdown list using VBA

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi EveryOne,
I want to make a vba code to hide the used items in dropdown list. I searched and got a possible code so I adjust some changes to fit my need but need help to complete this mission.
Column A of activesheet has a dropdownlist from column N in sheet"Data". If the name is selected, then the name of the dropdown list will shrink to have choice of those unused names for remaining cells in column A to choose.
Please help.

My code is here

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Sh.Name = "Data" Then Exit Sub

'DropDownList in Column A

Dim lr1 As Long
lr1 = Range("A3").End(xlDown).Row

Dim lr As Long
lr = Sheets("Data").Range("N1").End(xlDown).Row

Dim FullString As String
FullString = "='Data'!$N$2:$N$" & lr


Dim filledRng As Range
Dim newRng As Range
Dim v As String
Dim PartString As String

Set filledRng = Range("A3:A" & lr1)   
Set newRng = Range("A3:A" & lr1)


If Not Intersect(Target, newRng) Is Nothing Then

[COLOR=rgb(226, 80, 65)]v = filledRng.Value[/COLOR]
PartString = RemoveItem(FullString, v)

With Target.Validation
 .Delete
 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
 Operator:=xlBetween, Formula1:=PartString
 .IgnoreBlank = True
 .InCellDropdown = True
 .InputTitle = ""
 .ErrorTitle = ""
 .InputMessage = ""
 .ErrorMessage = ""
 .ShowInput = True
 .ShowError = True
End With


End If

End Sub



Public Function RemoveItem(st As String, drop As String) As String
    RemoveItem = Replace(Replace(st, drop, ""), ",,", ",")
End Function


TestDropDownList.xlsm
A
1MDate
2Date
3Zita V
4Warus O
5Robert M
6Jack S
7Ken C
8Mandy H
9Mary K
10Peter B
11Nacy L
12Larry Q
13John G
202202
Cells with Data Validation
CellAllowCriteria
A2List=List


TestDropDownList.xlsm
N
1Agent
2Bady B
3Cat G
4Jack S
5John G
6Ken C
7Larry Q
8Mandy H
9Mary K
10Nacy L
11Peter B
12Robert M
13Warus O
14Zita V
15Zita V
Data
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
It would work better without vba.
Book1
ANO
1MDateAgentdv_list
2DateBady BBady B
3Zita VCat GCat G
4Warus OJack S 
5Robert MJohn G 
6Jack SKen C 
7Ken CLarry Q 
8Mandy HMandy H 
9Mary KMary K 
10Peter BNacy L 
11Nacy LPeter B 
12Larry QRobert M 
13John GWarus O 
14Zita V 
15Zita V 
Sheet1
Cell Formulas
RangeFormula
O2:O15O2=IFERROR(INDEX($N:$N,AGGREGATE(15,6,ROW($N$2:$N$15)/ISERROR(MATCH($N$2:$N$15,$A$3:$A$13,0)),ROWS(O$2:O2))),"")
Cells with Data Validation
CellAllowCriteria
A3:A13List=OFFSET($O$2,,,COUNTIF($O$2:$O$15,"?*"))
 
Upvote 0
Hi jasonb75, Thanks. The column A name cells is a dynamic range, I prefer to use VBA.
 
Upvote 0
Hi, I tried to use two arrays to make two columns(N and O) in sheet('Data"). Is it possible to compare the values in two columns to find the shortfall then create another column next to them (Column P) to show those shortfall values. Then create dropdown list for Column A in activesheet.

VBA Code:
'In Sheet("Data')

Private Sub Worksheet_Change(ByVal Target As Range)
   'Sort Agent Name by alphabetic order
    Dim lr As Long
    Dim Agtarray As Object
    Dim cl As Range
    Dim Sorted_array As Variant
    
    'Creating a array list
    Set Agtarray = CreateObject("System.Collections.ArrayList")
    
    'Physical Source in Column L
    lr = Range("L1").End(xlDown).Row
    Debug.Print lr

    For Each cl In Range("L1:L" & lr)
        If Not Agtarray.contains(cl.Value) Then Agtarray.Add cl.Value
    Next cl
      
    Agtarray.Sort
    Sorted_array = Agtarray.toarray
          
    Range("N1").Resize(UBound(Sorted_array) + 1, 1).Value = Application.Transpose(Sorted_array)
 
End Sub


'In ActiveSheet

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Sh.Name = "Data" Then Exit Sub
If Target.CountLarge > 1 Or Target.Columns > 1 Then Exit Sub Then Exit Sub

'DropDownList in Column A
Dim lrA As Long
Dim colAarray As Object
Dim rngA As Range
Dim clA As Range
Dim SortedColA_array As Variant

'Creating a array list
Set colAarray = CreateObject("System.Collections.ArrayList")

'Physical Source in Column L
lrA = Range("A1").End(xlDown).Row
Debug.Print lrA
Set rngA = Range("A3:A" & lrA)


   For Each clA In rngA
      If Not colAarray.contains(clA.Value) Then colAarray.Add clA.Value
   Next clA
  
colAarray.Sort
SortedColA_array = colAarray.toarray

'Output SortedColA_array to Cells
Sheets("Data").Range("O2").Resize(UBound(SortedColA_array) + 1, 1).Value = Application.Transpose(SortedColA_array)

End Sub

TestDropDownList_2.xlsm
A
1MDate
2Date
3Zita V
4Viola C
5Robert M
6Warus O
7Ken C
8Mandy H
9Mary K
10Peter B
11Nancy L
12Larry Q
13Cat G
202202
Cells with Data Validation
CellAllowCriteria
A2List=List


TestDropDownList_2.xlsm
NOP
1AgentFrom ColumnAFind Missing bet N & O
2Cat GCat G
3Jack SKen C
4John GLarry Q
5Ken CMandy H
6Larry QMary K
7Mandy HNancy L
8Mary KPeter B
9Nacy LRobert M
10Peter BViola C
11Robert MWarus O
12Viola CZita V
13Warus O
14Zita V
Data
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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