I define some names in name manger like validation_list1 that refers to =OFFSET(Sheet3!$C$4,,,COUNTIF(Sheet3!$C$4:$C$399,"?*"))
I want to add validation list using vba . code below cause error 1004:
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=validation_list1"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
' Code with error
End With
but when I delete equal sign (=) from the text assigned to Formula1 every thing will be ok
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="validation_list1"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
'code without error!
End With
both above codes are similar but first one has Formula1:="=validation_list1" and second one has Formula1:="validation_list1" . why first one cause error 1004? I have to add Formula1:="=validation_list1" , how?
I do not have any problem with defining name manager , the problem is adding equal sign between quotation marks (the text assigned to Formula1 ). It does not matter what is the text , I tested it by Formula1:="=test" and caused error too then deleted = sign and wrote Formula1:="test" and everything were good.
Edit: Thanks chris neilsen . this piece of code is the output of Record Macro and if I manually select a cell and from Data tab and select Data Validation and enter the text =validation_list1 in Source field of list validation , everything will be ok but using vba causes error
SOLVED Thank you very much @chrisneilsen , your advices helped me to find the reason. Individually every things seem to be ok .formula , scope and named range were correct . This validation occurs after the program does some calculations and changes to sheets and formulas , so one cell which indirectly related to validation change to #REF . I used Define Name for formula of that cell and BINGO




=validation_list1in a cell?