Using VBA to Create Dependent Validations

FinProdpm

New Member
Joined
Jun 7, 2011
Messages
5
Hello All,

I've utilized these forums for some time now and have found them extremely helpful, however this is the first time I am posting a questions. I'd like to say thank you to anyone that has helped anyone out though, because helping one person probably helps 10x or 20x more people that read the post.

Now on to my issue... I am utilizing VBA to create two columns worth of validations. The first column (G) is created off of a named range "SC1_List", which has the following options {Security, Loan, Deposit, Borrowing}. I want the second column to be dependent based on the selection of the first column for the corresponding row (i.e. h2 dependent on g2, etc.), so I have four named ranges set up, as "SC2_List_Security", "SC2_List_Loan", etc. In VBA, I am using the following code to create the validation:


HTML:
'Set SC1 Validation
ActiveCell.Offset(0, 6).Select
With Selection.Validation
     .Delete
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=SC1_List"
    End With
'Set SC2 Validation
ActiveCell.Offset(0, 1).Select
    'Create text string for dependend validation formula
    strValidation = "=indirect(concatenate(""SC2_List_"",G" & i + 1 & "))"
With Selection.Validation
     .Delete
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=strValidation
     End With

I am getting an error 1004 application-defined or object defined error and I am not sure why.

I appreciate any help in advance. Thanks all.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Additionally, I've found that changing

HTML:
strValidation = "=INDIRECT(CONCATENATE(""SC2_List_"",G" & i + 1 & "))"

to

HTML:
strValidation = "INDIRECT(CONCATENATE(""SC2_List_"",G" & i + 1 & "))"

Does not result in an error, however my data validation is populated with the resulting text string only.
 
Upvote 0
My understanding is that the 1004 error, application or object definition is because the variables are defined as the wrong type of object. Is that correct?
 
Upvote 0
It seems like setting the dependent validation works if column G is populated. Is there anyway to force VBA to continue setting up the dependent validations, recognizing that the user will have to define column G after the validations are established?
 
Upvote 0
To anyone that reads this thread, I've found that while not necessary if setting up dependent validations in excel, setting up dependent validations in VBA requires the precedent validation be populated in order to create the dependent.

As a result, I established the precedent validation, populated it with a valid value, set up the dependent validation and then cleared the precedent validation. This seems to work well, although not ideal.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top