What I am missing here? The code should work as in single execution and also in a loop.
Public mySheet As Worksheet
Set mySheet= Sheets("CARS")
Error appears in this line-->
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(optionList, ",")
Error is-->
VBA Runtime Error 1004 “Application-defined or Object-defined error
Public Function addDataValidation(row As Long)
Dim optionList(2) As String
optionList(0) = "1"
optionList(1) = "2"
optionList(2) = "3"
With mySheet.Cells(row, 3).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(optionList, ",")
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=1")
.Font.Bold = True
.Interior.ColorIndex = 4
.StopIfTrue = False
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=2")
.Font.Bold = True
.Interior.ColorIndex = 6
.StopIfTrue = False
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=3")
.Font.Bold = True
.Interior.ColorIndex = 3
.StopIfTrue = False
End With
With mySheet.Cells(row, 3)
.HorizontalAlignment = xlCenter
.Value = optionList(0)
End With
End Function
The reason for illogical behavior was that the function messed up with equally named function in a different class module.
Solution is 1
Private Function addDataValidation(row As Long)
Solution 2
Check always cells protection status.