Vincent88
Active Member
- Joined
- Mar 5, 2021
- Messages
- 382
- Office Version
- 2019
- Platform
- Windows
- 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
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 | |||
1 | MDate | ||
2 | Date | ||
3 | Zita V | ||
4 | Warus O | ||
5 | Robert M | ||
6 | Jack S | ||
7 | Ken C | ||
8 | Mandy H | ||
9 | Mary K | ||
10 | Peter B | ||
11 | Nacy L | ||
12 | Larry Q | ||
13 | John G | ||
202202 |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
A2 | List | =List |
TestDropDownList.xlsm | |||
---|---|---|---|
N | |||
1 | Agent | ||
2 | Bady B | ||
3 | Cat G | ||
4 | Jack S | ||
5 | John G | ||
6 | Ken C | ||
7 | Larry Q | ||
8 | Mandy H | ||
9 | Mary K | ||
10 | Nacy L | ||
11 | Peter B | ||
12 | Robert M | ||
13 | Warus O | ||
14 | Zita V | ||
15 | Zita V | ||
Data |