Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my code for doing exactly what you are asking.</p> <p>I have defined the independent column as a named range called <code>Major_Category</code> and set the drop down validation to a data list. I then have several other data lists that are named <code>cat_subItems</code>. So, for your example the major category would have items</p> <ul> <li>fruit</li> <li>vegetable</li> </ul> <p>then I defined to more lists called</p> <ul> <li>cat_fruit</li> <li>cat_vegetable</li> </ul> <p>which would contain the names of the fruits or vegetables. Then based on the <em>major category</em> selection the Worksheet_change event will change the drop down validation in the next column over to either <code>cat_fruit</code> or <code>cat_vegetable</code>.</p> <p><strong>Note:</strong> This code does not play nice if you are using excel's protect worksheet. See <a href="https://stackoverflow.com/questions/445519/how-do-i-avoid-run-time-error-when-a-worksheet-is-protected-in-ms-excel">this question</a> for dealing with Excel's worksheet/book protection.</p> <pre><code>Public Sub Worksheet_Change(ByVal target As Range) On Error GoTo ErrHandler: Dim VRange As Range, cell As Range Dim msg As String Dim validateCode As Variant Dim modCell As Range Set VRange = Range("Major_Category") If Intersect(VRange, target) Is Nothing Then Exit Sub For Each cell In Intersect(VRange, target) b = cell.Value curRow = target.Row Set modCell = cell.Offset(0, 1) 'cell to modify the validation' If Not (b = "") Then modCell.Validation.Delete modCell.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 'sets the validation formula to the list/range name' xlBetween, Formula1:="=cat_" &amp; b modCell.Validation.IgnoreBlank = True modCell.Validation.InCellDropdown = True modCell.Validation.InputTitle = "" modCell.Validation.ErrorTitle = "" modCell.Validation.ErrorMessage = "" modCell.Validation.ShowInput = True modCell.Validation.ShowError = True End If Next cell Cleanup: Exit Sub ErrHandler: MsgBox Err, vbOKOnly, "Error Occurred" Resume Cleanup: End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload