[英]List validation for a cell
I am trying to set list validation for a cell via VBA.我正在尝试通过 VBA 为单元格设置列表验证。
I use a Formula1:= when the formula is a string.当公式是字符串时,我使用 Formula1:=。
When I try to use & to combine variables, it is giving me a 400 error.当我尝试使用 & 组合变量时,它给我一个 400 错误。
The issue with the version of Formula1 with concatenated variables seems to be that the reference cell has an #N/A value.具有串联变量的 Formula1 版本的问题似乎是参考单元格具有 #N/A 值。 The written formula doesn't have that problem.
书面公式没有这个问题。
Sub InsertRow()
r = Worksheets("Kickoff Schedule").UsedRange.Rows.Count + Worksheets("Kickoff Schedule").UsedRange.Rows(1).Row - 1
...
With Range("E" & r).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="=INDIRECT($D" & CStr(r) & ")"
End With
End Sub
Full list of things I've tried and if they have/haven't worked:我尝试过的事情的完整列表以及它们是否有效:
' Works:
Formula1:="=INDIRECT($D19)"
' Works:
MsgBox "=INDIRECT($D" & CStr(r) & ")"
' MsgBox shows =INDIRECT($D19) , as expected
' Doesn't work:
Formula1:="=INDIRECT($D" & CStr(r) & ")"
' Doesn't work:
Formula1:="=INDIRECT(""" & Range("D" & r).Address(False, False) & """)"
' Shows one option with a string of the right formula, as expected:
Formula1:="INDIRECT($D" & CStr(r) & ")"
I have already read Using Indirect function in Data Validation through VBA我已经阅读了通过 VBA 在数据验证中使用间接函数
You do not need to use the INDIRECT function in VBA to create a formula or cell reference from text.您不需要使用 VBA 中的 INDIRECT 函数从文本创建公式或单元格引用。 Just do this:
只需这样做:
Formula1:="=$D" & CStr(r)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.