我正在尝试基于字符串作为地址在单元格中创建数据验证。该字符串是从工作表中保存的。
在此示例中,strAddress
返回$G$3:$G$14
。
我明白了
对象定义的错误
添加代码中以*表示的验证时。
这是例行程序的一半,其余的都是重复的:
Sub UpdateLists(advertiser As String)
' On Error Resume Next
'need to update the other data validation based on the advertiser selected
Dim strAdvertiser As String, strAddress As String
Dim adRng As Range
Dim myRng As Range
'clear validation first
Range("I10:I12").Validation.Delete
strAdvertiser = advertiser
'now find the relevant supplier in the lists sheet
Set adRng = Sheets("Lists").Range("A:A").Find(What:=strAdvertiser)
If Not adRng Is Nothing Then
'adjust division according to advertiser
Set myRng = ActiveSheet.Cells.Find(What:="Division")
If Not myRng Is Nothing Then
'list range
strAddress = adRng.Offset(0, 1).Text
'adjust list
'**error on below line
myRng.Offset(0, 1).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Lists'!" & strAddress
Else
MsgBox "Failed to find Division", vbCritical
Exit Sub
End If
End if
End Sub
我尝试过:
a)删除myRng
对象并使用Sheets
,但这仍然会返回错误。
b)对strAddress
进行硬编码。
c)在Debug.Print
对象上运行多个myRng
,以检查其有效性。
d)最初使用.Validation.Modify
,但随后又移至.Validation.Delete
和.Validation.Add
。
答案 0 :(得分:2)
我认为您必须先清除所有现有的验证,然后再将验证设置为列表。
尝试将代码更改为
With myRng.Offset(0, 1)
.Validation.Delete
.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Lists'!" & strAddress
End With