Data Validation Text List using VBA

Excelacity

New Member
Joined
Apr 5, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a drop down validation list from a dynamic range on another sheet. Having read lots of posts I end up with the code below but cannot understand why the code line
".Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=FullList" is causing a problem. Any help would be much appreciated

VBA Code:
Dim ListOrder As Byte
Dim StrName() As Variant
Dim FullList As String
Dim RngList, CellNow As Range
Dim Wb As Workbook
Dim WsFrom, WsTo As Worksheet
Set Wb = ThisWorkbook
Set WsTo = Wb.Worksheets("CurrentWeek")
Set WsFrom = Wb.Worksheets("Info")
Set RngList = WsFrom.Range("ListIrisID") 'Dynamic range name
ReDim StrName(RngList.Cells.Count)
'Build array list
For Each CellNow In RngList.Cells
StrName(ListOrder) = CellNow.Value
ListOrder = ListOrder + 1
Next CellNow
FullList = ""
'Create single comma separated string of entire ID List
For ListOrder = LBound(StrName) To UBound(StrName)
Debug.Print StrName(ListOrder)
FullList = FullList & StrName(ListOrder) & ","
Next ListOrder
FullList = Left(FullList, Len(FullList) - 2)
'Test
Debug.Print Len(FullList); FullList

'Add Validation list to cells
With WsTo.Range("E8:E207").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=FullList
.IgnoreBlank = True
.InCellDropdown = True
End With
 
As far as I know, using strings as the source of data validation (like your code does) is limited to 255 characters max.
If the string length is more than 255 characters then initially the data validation will work but when you save the workbook then it won't work.

For dynamic range, here's an example of formula to define the named range:
=Sheet1!$A$1:INDEX(Sheet1!$A:$A, COUNTA($A:$A), 1)

then use the named range as the source of data validation.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
The problem is the length of the string you're putting into the DV.
Is the dynamic name range with the data set at worksheet or workbook scope?
 
Upvote 0
The problem is the length of the string you're putting into the DV.
Is the dynamic name range with the data set at worksheet or workbook scope?
The dynamic name range is set at workbook level. The dynamic range is on the same workbook but a different worksheet (protected) to the data that needs the validation. The Dynamic range name was already set up in a similar fashion to Akuini's suggestion.
Initially, I tried to populate the data validation directly from the dynamic range, but couldn't work out the code to do that, which Is why I tried to populate the DV using a VBA array. I would appreciate it if anyone could suggest code to populate the DV directly from the dynamic range.
 
Upvote 0
How about
VBA Code:
With WsTo.Range("E8:E207").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=ListIrisID"
.IgnoreBlank = True
.InCellDropdown = True
End With
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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