简体   繁体   中英

VBA to check value against cell validations

I have an Excel form with complex cascaded validations. This means that A1 entries are limited to a specific dropdown list. The same for A2, A3 etc. Option selected in A1 determines options available for A2, options under A3 are conditioned by choices of A2 (and perhaps A3 too) etc.

The form works for manual entry, but I would like to automate some user inputs using a simple macro (A2.Value = ...), however when I do so, the entered value overrides the validations.

Is there a way to evaluate a value against the list of possible validations within the cell? The validation rules are complex and I am reluctant to recreate them in VBA. I have also found that it is possible to verify if the value already entered in a cell corresponds to its validations, but again, this is not ideal due to the complexity of validation rules (eg if at some stage a value does not correspond to validations, I need to stop the macro altogether and not continue with its execution).

addendum:

OK, so example:

A1 validation is a simple list: "A, B"

A2 validation is a list defined by formula pointing to named ranges:

=IFS(A1="A",INDIRECT(NamesStartingByA),A1="B",INDIRECT(NamesStartingByB))

This means that if in A1, I select option "A", in A2, I will be able to select "Andrea, Alena, Anna,..."; if I select "B", A2 will offer "Bob, Boris, Barbara,..."

This works for manual input well, ie selecting "A" in A1 prevents the user to put value "Bob" into A2.

However I am writing a macro that allows the user to input values en masse from their own spreadsheet

wsTarget.Cells(1,1) = wsSource.Cells(1,1)
wsTarget.Cells(2,1) = wsSource.Cells(1,2)

However if the user put in his source sheet "A" and "Bob", the code will override the validations. This is not an unexpected behaviour for me but I would like to know if there is a way to check if wsSource.Cells(1,2) corresponds to the conditinally determined dropdown in wsTarget.Cells(2,1) prior to putting in the value.

For these sorts of cases, I always prefer to use a UserForm. For two reasons:

  1. It is more user-friendly.
  2. It is easier to 'protect' and script the options that you need.

For instance, a UserForm that I made consisted of a couple of courses that, if checked completely, would open a variety of learning material for follow-up courses (so what is viewed is also dependent on previous answers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM