我有一个加载了动态结果数据集的 excel 表。加载完所有数据后,我需要在每行末尾添加一个“是/否”下拉列表。我必须动态执行此操作,因为我事先不知道结果集的大小。以下代码引发“应用程序定义或对象定义错误”:
Dim firstRow As Integer
Dim lastRow As Integer
Dim I As Integer
Dim VOptions As String
VOptions = "1. Yes, 2. No"
firstRow = GetResultRowStart.row + 1
lastRow = GetResultRowStart.End(xlDown).row
For I = firstRow To lastRow
Range("AO" & firstRow & ":AO" & lastRow).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=VOptions
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Options"
.ErrorTitle = ""
.InputMessage = "Click yes or no"
.errorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next I
GetResultRowStart 方法为我提供了表格中填充结果数据的起始行。我在代码的其他部分的其他地方也使用了这种方法,并且效果很好。使用消息框进行调试建议在 Range(..).select 语句中引发错误。
有关此错误原因的任何想法。