In previous articles I have written about the connection between Excel UDFs and objects, I have noted that almost any object can be invoked through its use. So, it was with interest that I read a recent article on Debra Dalgleish’s website on the accidental deletion of dropdown arrows in cells with data validation lists when running macros.
In the article, she stated
“If you run a macro that deletes shapes on a worksheet, it might also delete the drop down arrow. Excel sees that arrow as a worksheet shape.”
Well, I just wrote this article where a UDF invoked filter arrows to emulate the properties of Excel new FILTER function.
So, I figured that, just maybe, a UDF could do the same thing with data validation. It CAN! And, it can add the data validation to any desired worksheet cell.
The following code is for the AddValidation VBA UDF:
Function AddValidation(vRange As Range, vList As Range)
On Error Resume Next
Range(vRange.Address).Validation.Add Type:=xlValidateList, _
Formula1:=”=” & vList.Address
Its use can be seen in the following figures. In the first, the formula
=AddValidation(A3,letters) is in cell F5. Actually, in the figure the 2nd argument in the UDF is J1:J3, but that is equivalent to the defined name range called letters.
The first argument points to cell A3 as the target for adding the data validation list. In the 2nd figure, you can see that the data validation drowdown arrow appears in A3. Pretty amazing!
I think you can see the myriad of possibilties that are possible by using this technique.
Free free to download the sample workbook at the link shown below. HTH!