I'm trying to use VBA to add a drop down list that can vary in length dependent on the size of an array. I'm getting no error for the following code, but it also does not actually add the data validation...
To form the list:
Dim RegionList as String
RegionList = ""
For i = LBound(x) To UBound(x)
RegionList = RegionList + x(i) & ", "
Next
To create the validation:
RegionList = Left(RegionList, Len(RegionList) - 2) 'Remove trailing Comma/Space
With Sheet2.Range("C1").Validation 'Insert dropdown to cell C1
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=RegionList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
I should note that the sheet is protected prior to running this code with the following formula:
Sheet2.Protect Password="xxx", DrawingObjects = False, UserInterfaceOnly = True
Edit: The specific cell where I want the drop down list to appear is unprotected, so the bit about protecting the sheet is probably irrelevant.
Any ideas would be appreciated!
Private Sub Worksheet_SelectionChange(ByVal Target As Range). In other words, what is telling your code to perform this action? Do you have a button, another event handler, etc?