简体   繁体   中英

Excel - drop down menu changes with certain input in another cell

I have two cells for drop-down menu. Cell A1 is for brand name and B1 is for model name. I want that if in cell A1 Samsung is selected that in cell B1 only Samsung models to be enable. All the brand and models are listed in separated sheet.

Please let me know how to implement this as I am new to excel.

The solution is actually simpler than what you may think. I've made 2 suggestions below, the first doesn't require VBA at all – the second one does. In both cases, you must start by correctly naming the Ranges where the model names are listed.

Name the Ranges (must be done for both options)

Using Samsung as the example, select the cells where all the Samsung model names are listed (must be contiguous) and name the range Samsung . Repeat for all the other model listings by brand name. You say that All the brand and models are listed on separated sheet so you know how to do this.

Option One – formula only

In cell B1 , set the Data Validation as such: Allow: = List . Source: = =INDIRECT($A$1) Now, when you change the selection in cell A1 , the Source of the Data Validation changes to that brands' model list via the named range.

Pros: doesn't require VBA. Simple to implement.

Cons: INDIRECT() is a Volatile function. When you change brands in A1 , the last model used is left in cell B1 (until you change it) which may interfere with any formulas that reference the cell.

Option Two – VBA

Place the following code in the Worksheet Module area of the sheet where your Data Validation cells are.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo GetOut
Application.EnableEvents = False
Dim MyList As String

If Not Intersect(Range("A1"), Target) Is Nothing Then
 With ActiveSheet.Range("B1")
    .ClearContents
    .Validation.Delete
    MyList = Sheet1.Range("A1").Value
    .Validation.Add Type:=xlValidateList, Formula1:="=" & MyList
End With
ActiveSheet.Range("B1").Value = ActiveSheet.Range(MyList).Cells(1, 1).Value
End If

Continue:
    Application.EnableEvents = True
    Exit Sub
GetOut:
    MsgBox Err.Description
    Resume Continue   
End Sub

Pros: replaces the value in B1 with the first valid model number for that brand.

Cons: uses VBA so the file needs to be saved as a macro-enabled file type

Let me know how you go.

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