我正在尝试使用 VBA 添加一个下拉列表,该下拉列表的长度取决于数组的大小。以下代码我没有收到任何错误,但它实际上也没有添加数据验证......
要形成列表:
Dim RegionList as String
RegionList = ""
For i = LBound(x) To UBound(x)
RegionList = RegionList + x(i) & ", "
Next
要创建验证:
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
我应该注意,在使用以下公式运行此代码之前,工作表受到保护:
Sheet2.Protect Password="xxx", DrawingObjects = False, UserInterfaceOnly = True
编辑:我希望下拉列表出现的特定单元格不受保护,因此有关保护工作表的部分可能无关紧要。
任何想法,将不胜感激!