Got any Excel/VBA Questions? Free Excel Help
Decreasing Data Validation Lists
If you need to have a list of options that decreases as users select from it, you can use data validation and some Excel VBA code.
On any Worksheet add your list, say in A1:A10. Now select this range and click in the Name Box (left of the Formula bar), type the name: MyList and then Enter. This will give you a named range called MyList.
Now select the range on another Worksheet where you would like the Validation Lists to go. Now right click on this sheet name tab, select View Code and paste in the code below:
Private Sub Worksheet_Change(ByVal Target As Range) Dim strVal As String Dim strEntry As String On Error Resume Next strVal = Target.Validation.Formula1 If Not strVal = vbNullString Then strEntry = Target Application.EnableEvents = False With Sheet1.Range("MyList") .Replace What:=strEntry, Replacement:="", _ LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False .Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlNo, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom .Range("A1", .Range("A65536").End(xlUp)).Name = "MyList" End With End If Application.EnableEvents = True On Error GoTo 0 End Sub
You should note that I have referred to the named range MyList as Sheet1.("MyList"). I have preceded the named range with its sheet code name. The reason for this is that the reference to the named range (MyList) is in the Private Module of another Worksheet. Without it, Excel would assume MyList
|Return The Weekday of Date in Excel|
|Debug VBA Code|
|Debug, Evaluate/Audit & Fix Excel Formula Errors|
Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft.