简体   繁体   中英

List validation for a cell

I am trying to set list validation for a cell via VBA.

I use a Formula1:= when the formula is a string.

When I try to use & to combine variables, it is giving me a 400 error.

The issue with the version of Formula1 with concatenated variables seems to be that the reference cell has an #N/A value. 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

You do not need to use the INDIRECT function in VBA to create a formula or cell reference from text. Just do this:

Formula1:="=$D" & CStr(r)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM