Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim iRow As Long
For iRow = 2 To LastRow
AddListValidation ws.Cells(iRow, "C"), ws.Cells(iRow, "B")
Next iRow
End Sub
Sub AddListValidation(cellSource As Range, cellTarget As Range)
cellTarget.Value = "Select your values here"
With cellTarget.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=cellSource.Value
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
首次填充下拉列表。当我在工作表上进行一些更改时,当我再次重新打开工作表时,它说它已损坏并且也没有获得下拉列表。
任何人都可以帮我吗?
我使用的公式是否正确?
.Add Type:=xlValidateList, Formula1:=cellSource.Value
答案 0 :(得分:0)
cellSource
很可能是空的,因此它会在Formula1
给出错误,它会尝试获取其值。
在Sub AddListValidation
这一行之后写下你的代码:
If Len(cellSource) Then Exit Sub
因此,如果cellSource
为空并且将避免错误,它将退出。