我试图遍历一段代码,为同一列和后续列中的一组特定单元格设置验证列表。我遇到的问题是.ADD行上的“运行时错误1004应用程序定义的错误或对象定义的错误”,该行具有我的validatelist,alertStyle ... etc到具有我的
的公式Formula1:="=OFFSET($A" & t & "$1,0,0,COUNTA($A" & t & ":$A" & t & "),1)"
我不确定这是否是我的问题。这是我所有的代码。欢迎您对问题是什么或总体改进有任何见解。
If Cells(2, 2) = "Some Company Name" Then
t = "O"
For i = 3 To 50
Set CRange = Range(Cells(10, i), Cells(50, i))
Range(Cells(10, i), Cells(50, i)).ClearContents
Range(Cells(10, i), Cells(50, i)).Validation.Delete
Range(Cells(10, i), Cells(50, i)).Value = ""
For Each cell In CRange
With Range(Cells(10, i), Cells(50, i)).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=OFFSET($A" & t & "$1,0,0,COUNTA($A" & t & ":$A" & t & "),1)"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = "If the Test name you are looking for is not present please contact a Equipment Handler"
.ShowInput = True
.ShowError = True
End With
Next
t = Chr(Asc(Letter) + 1)
Next i
End If
因此,我对代码进行了一些更改,从而弄清楚了问题。我认为发生的事情是由于经过AZ列而发生了错误。当将i to 50
仅限制为另一个变量时,我已经不小心设置了它。另外,我搞砸了t= Chr(Asc(Letter)+1)
,应该把t = Chr(Asc(t) + 1)
放在正确的代码下面。感谢您的所有帮助。
If Cells(2, 2) = "Some Company Name" Then
t = "O"
Val1 = Cells(9, 2) + 2
For i = 3 To Val1
Range(Cells(10, i), Cells(50, i)).ClearContents
Range(Cells(10, i), Cells(50, i)).Validation.Delete
Range(Cells(10, i), Cells(50, i)).Value = ""
With Range(Cells(10, i), Cells(50, i)).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=OFFSET($A" & t & "$1,0,0,COUNTA($A" & t & ":$A" & t & "),1)"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = "If the Test name you are looking for is not present please contact a Equipment Handler"
.ShowInput = True
.ShowError = True
End With
t = Chr(Asc(t) + 1)
Next i
End If`