0
votes

I have a excel that im adding a data validation.

When I use the Data Validation process trough the excel function it works fine:

Custom Formula : =IF(AND(M7>=DATE(2015;1;1);M7<=D7);TRUE;FALSE)

But when i use VBA code to add that data validation the following error occurs:

Run-Time error '1004':

Application-defined or object-defined error

VBA:

Range("M7:M" & (6 + 15)).Select

With Selection.Validation
    .Delete
    .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=IF(AND(M7>=DATE(2015;1;1);M7<=D7);TRUE;FALSE)"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = False
    .ShowError = True
End With

What im doing wrong ?

Thank you for the help.

2

2 Answers

2
votes

Replace all the ; (semicolons) with , (commas). The macro recorder is dim-witted sometimes. In the VBA code, you must use the US english Excel markup: . (dot) for a decimal, and , (comma) as parameter separator in formulas.

Like this:

.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _ 
 xlBetween, Formula1:="=IF(AND(M7>=DATE(2015,1,1),M7<=D7),TRUE,FALSE)"
-1
votes

Check that formula1 for validity. My guess is that it's not formatted correctly and excel's built in formula validation won't let it enter in an incorrect formula - hence the error