I am trying to set list validation for a cell via VBA. I'm able to use a Formula1:= when the formula is just a string, but when I try to use & to combine in variables, it's giving me a 400 error. The issue with the version of Formula1 with concatenated variables seems to be having issue with the fact that the reference cell has an #N/A value, but the written formula doesn't have that problem. Here's my latest try:
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) & ")"
What am I doing wrong? Thanks in advance!
P.S. I have already read Using Indirect function in Data Validation through VBA