Dynamic validatation based on previous validation list

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
OK, here is what I am trying to accomplish: I have one cell (M12) that uses a data validation list that is tied to all the names of the worksheets in the workbook. I want the user to be able to select the worksheet they want to use in the first validation list, then have a second validation list update with data from the selected sheet. I have the following code, but something is not right.

Code:
Sub ValidateOptions()
Dim sheetname As String
Dim last

sheetname = Range("M12").Text
last = Range("X1").End(xlDown).Offset(0, 0).Row

    Range("M13").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="='sheetname'!$X$1:$X$" & last
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub
I get a 1004 application defined or object defined error on this line:
Code:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="='sheetname'!$X$1:$X$" & last
I think it is in my Formula1:= section, something not right with how I am calling 'sheetname' into the code.

Any help is greatly appreciated! Thanks!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Put this line in this way:

Formula1:="=" & sheetname & "!$X$1:$X$" & last
 
Upvote 0
If your first list (for cell M12) has a source like Sheet1, Sheet2, Sheet3

You could use =INDIRECT($M$12&"!$X$1"):INDEX(INDIRECT($M$12&"!$X:$X"),MATCH("zzz",INDIRECT(A1&"!$X:$X"),1)
as the source for the second validation list.

If your second source has numbers instead of text, use 99E+99 in place of "zzz"
 
Upvote 0
Thanks for the quick reply! My source for the first list is the name of the sheets in the workbook, which are not as simple as Sheet1, Sheet2, but are something like this: Traditions 1" and 2⅜" Woods, DayDream ¾ Sngl& ⅜ Dbl SlideVue, etc. So a little more complex. Not sure if that makes a difference. Also, I know my way around excel pretty well, but I know nothing about the INDIRECT, INDEX, and MATCH functions. Can you explain them a little so I can understand what they are doing? Thanks again, I really appreciate it.
 
Upvote 0
INDIRECT("Sheet1A1") returns the cell Sheet1!$A$1
INDEX returns a subrange of the first argument INDEX(Sheet1!B:B,3,1) returns Sheet1!B3
MATCH matches a value against an array of values MATCH("zzz", Sheet1!C:C) returns the last text entry in Sheet1!C:C.

More info can be found in HELP.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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