Search code examples
excelvba

Validation Lists in Generated Table with VBA


I have code that generates a table using VBA. I need the table to have drop down lists within certain columns. I hade some code that was working, but now it is no longer working. No idea why, and oddly the exact code is working for one column, but not the other 2. Can someone tell me if there is something wrong with this code???

Note: The first 2 With statements fail and dont populate the drop down in the table, but the 3rd with statement does work.

With objTable.DataBodyRange.Columns(9).Validation
             .Delete
             .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=Services"
             .IgnoreBlank = True
             .InCellDropdown = True
             .InputTitle = ""
             .ErrorTitle = ""
             .InputMessage = ""
             .ErrorMessage = ""
             .ShowInput = True
             .ShowError = True
End With

With objTable.DataBodyRange.Columns(9).Validation
             .Delete
             .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=Status"
             .IgnoreBlank = True
             .InCellDropdown = True
             .InputTitle = ""
             .ErrorTitle = ""
             .InputMessage = ""
             .ErrorMessage = ""
             .ShowInput = True
             .ShowError = True
End With

With objTable.DataBodyRange.Columns(14).Validation
             .Delete
             .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=AOR"
             .IgnoreBlank = True
             .InCellDropdown = True
             .InputTitle = ""
             .ErrorTitle = ""
             .InputMessage = ""
             .ErrorMessage = ""
             .ShowInput = True
             .ShowError = True
End With

Solution

    • For xlValidateList, Formula1 is required, Formula2 is ignored. Operator argument doesn't change the output.

    Microsoft documentation:

    Validation.Add method (Excel)

    With objTable.DataBodyRange.Columns(9).Validation
                 .Delete
                 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                   Formula1:="=Services"
                 .IgnoreBlank = True
                 .InCellDropdown = True
                 .InputTitle = ""
                 .ErrorTitle = ""
                 .InputMessage = ""
                 .ErrorMessage = ""
                 .ShowInput = True
                 .ShowError = True
    End With
    

    enter image description here