I have a worksheet used for data entry, a kind of sandbox tool to plan work. So the data set grows and shrinks as the work is planned and the plan is reworked, and optimized. The user often enters a list of tasks, then reorders the list, the inserts tasks in the middle of the list, and I need to add some validations to account for those moves. My solution was to set the data validation in the first cell in the column and copy and paste just the validation down the data set each time a row is added.
I am attempting to used data validation to force identical tasks to have the same duration. When I hand enter the data validation it works, and I've recorded a macro to double check my code. I've also tried replacing the formula with a variable defined as a string. But I can't get VBA to assign the data validation.
I am calling the following Sub when some of those moves occur. But I am getting a Run-time error 1004 on the .Add line. Doesn't like the formula or one of the parameters.
Sub Validate_Dur()
Dim Last_Row As Long
Last_Row = Sheets("Template").Cells(Rows.Count, 2).End(xlUp).Row ' catches the new last row
Range("D3").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateCustom, _ ' this is where I'm having trouble
AlertStyle:=xlValidAlertStop, _
Formula1:="=AND(B3=B2,D3=D2)"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Duration Error"
.InputMessage = ""
.ErrorMessage = _
"Tasks with identical descriptions must have the same duration."
.ShowInput = True
.ShowError = True
End With
Selection.Copy
Range("D3" & Last_Row).Select
Selection.PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
Any help?