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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What problems do you get?
Also what is the length of FullList?
 
Upvote 0
What problems do you get?
Also what is the length of FullList?

There's an image of the error. The length of FullList is 304 characters.
Capture.JPG
.
 
Upvote 0
What line of code triggered the error?
 
Upvote 0
Is the worksheet protected?
 
Upvote 0
In that case you will need to unprotect the sheet before adding the DV & then reprotect it afterwards
 
Upvote 0
The validation goes on fine if I remove protection & re-apply all with VBA. If I then save the file & re-open it, the file gets corrupted on opening, loses all the sheet formatting and DV. I have tried taking protection off completely and it still gets corrupted after running the code. Any offers on what is wrong. The code as it stands now is below. Additional information - I have hidden columns from T onwards & hidden rows from 208 downwards.
VBA Code:
With Worksheets(StrWsTo).Range(StrRngDvalName).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=FullList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Engineer ID"
.ErrorTitle = "Invalid Entry"
.InputMessage = "Type or select Engineer's IRIS Diary ID from in-cell drop down"
.ErrorMessage = "This name is not on the IRIS Diary ID list, please try again"
.ShowInput = True
.ShowError = True
End With
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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