Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel: Call a procedure from Data > Validation? 1

Status
Not open for further replies.

mikevh

Programmer
Apr 23, 2001
1,033
US
Hi --

One of the columns in my worksheet calls for fancier
validation than I could figure out how to do with any
of the options in Data > Validation. I wrote a procedure
to do it and I'm calling it from the sheet's
SelectionChange event. This works okay, but I'm
wondering if I could somehow call it from
Data > Validation instead, say from "Custom", perhaps?
I couldn't figure out any way of specifying the procedure there that didn't either result in an error message or
just do nothing.

Thanks.
 
You can't put a custom formula directly in the custom validate, but you can reference a cell that has a custom formula.

For example, if Cell A1 has the custom validation rule
Code:
   =C1
and cell C2 has the formula:
Code:
   =MyValidate(A1,B2)
And MyValidate is a user-defined function such as
Code:
Option Explicit
Function MyValidate(x, TestCell As Range) As Boolean
Code:
  ' If TestCell is yellow, then x must be <= 13
  ' otherwise x must be <= 6
Code:
  If TestCell.Interior.ColorIndex = 6 Then
    If x > 13 Then
      MyValidate = False
    Else
      MyValidate = True
    End If
  Else
    If x > 6 Then
      MyValidate = False
    Else
      MyValidate = True
    End If
  End If
End Function
Try to enter numbers > 6
Change the background color of cell B2 to yellow and try again up to >13.

As you can see, you can make the function as complex as you need. If still more parameters are needed, just add them to the list.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top