我正在尝试使用 VBA 宏从存储在我的工作表中的列表中设置单元格数据验证。我不知道列表会有多长,所以需要动态选择范围。
目前 .Add Type:= Formula1:="=perfGradeRange" 行正在引发运行时错误“1004”应用程序定义或对象定义错误。
我的代码是这样的:
Sub Perf_Grade_Dropdown()
Dim perfGradeData As Worksheet
Dim usedRange As range
Dim rLastCell As range
Dim range As range
Dim perfGradeRange As range
Set perfGradeData = Worksheets("Values")
perfGradeData.Unprotect Password:="pass"
perfGradeData.Activate
Set rLastCell = perfGradeData.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
Set perfGradeRange = perfGradeData.range(Cells(1, 1), rLastCell)
Set range = perfGradeData.range(Cells(3, 3), Cells(4, 3))
With range.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=perfGradeRange"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
perfGradeData.Protect Password:="pass", DrawingObjects:=True, contents:=True, Scenarios:=True, userinterfaceonly:=True, _
AllowSorting:=True, AllowFiltering:=True, AllowDeletingColumns:=True, AllowInsertingColumns:=True
perfGradeData.EnableAutoFilter = True
End Sub
我已经看到了以下两个问题,但未能得到任何可行的建议:
当工作表在 MS-Excel 中受到保护时,如何避免运行时错误?
感谢任何帮助。