I need help with clearing/changing a dependent drop down list in excel 2013, without using a macro, if it's possible. I've tried different workarounds, but nothing is working:
This is one of the things I tried:
=INDIRECT(VLOOKUP($A$8, lookuptable, 2, FALSE))
This doesn't work, the dependent (child) drop down cell is not being cleared/changed when the parent (A8) drop down is changed, causing a miss-match.
Thanks!
The technique for dependent data validation is to use range names for each dependent data list. The range name is the same as the value selected in the parent list. Example: the parent data validation cell has a list of countries.
You also build several lists, one for each country, with the cities of that country. The range names for each list are the country names.
Apply the country DV to A1.
Then build another range name to use in the dependent data validation for the city cell, e.g. ListRange. The formula to build the range name is
=Indirect($A$1)
The dependent data validation then points to the range that has the same name as the value in cell A1. This is a dynamic range and will change when the selection in cell A1 changes.
This will not clear any existing choice in the dependent data validation cell. E.g, if your first DV cell has a list of countries and the dependent DV has a list of cities, selecting a country in A1 will change the DV list for the city cell and a city can be selected (France > Paris). But if the country cell is changed to Italy, the city cell will still show Paris, while the DV drop down now lists cities in Italy.
In order to clear the selected city when the country cell is changes, you will indeed need VBA.
This is the best I have come up with, pick the country in cell A2 and the city will change BUT only according to the position specified in cell A4. I hope it may help you, cheers.
I have a sample piece of code I use for a similar instance, one dropdown list with 3 options using an indirect formula to provide different choices per option.
I use named ranges and indirect formula via data validation.
When I change my list value in my dropdown cell "D2" in this example, it will then clear the second dropdown list in "F2" (2 columns to the right or offset by 2). This then enables the user to select from new choices dependant on the option change...
Hope this helps.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$2" Then
If Target.Validation.Type = "$F$2" Then
Application.EnableEvents = False
Target.Offset(0, 2).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Related
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.
I'm new to excel but now i'm stuck at something.
The one thing i try to achieve is that if i add a specific word into a textbox, another cell gets +1 (so if 0 and text has been entered in the textbox, it changes to 1 etc.)
so for example:
Cell B2 = Apple
Cell H2 : value of B2
I'd like to get, if possible, one or two textboxes where i could put the type of product and another box for the amount.
Thanks in advance.
OK. Here's a solution.
Set up a named range. I entered 5 different fruit in one column and 5 different quantities in the adjacent column (doesn't matter where but must be adjacent). I named the range "Products" but any other name will do just as well.
I set up a data validation list. I used cell G3 but any other will be equally suitable. I pointed the data validation list to =INDEX(Products,0,1), meaning the first column of the Products range.
Now I added code to the worksheet. This code must be in the code sheet of the worksheet on which G3 is located. That code sheet will have a name like Sheet1 (Sheet1). Don't use a standard code module with a name like Module1. Here is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
' 018
Const Trigger = "G3" ' change to suit
Dim Qty As Long
With Target
If .Address = Range(Trigger).Address Then
On Error Resume Next ' in case not found
Qty = Application.VLookup(.Value, Range("Products"), 2, False)
.Offset(0, 1).Value = Qty + 1
End If
End With
End Sub
Note that the Trigger cell is named as "G3". This must be the cell where you have the data validation drop-down.
This code will run whenever Trigger is changed. You make a selection there and the VLOOKUP function will find the quantity in column 2 of the Products range. If the item isn't found in the list it will return 0 but you can set the cell validation to prevent the entry of an item that isn't in the list. The code will add one to the quantity found and issue the result in .Offset(0, 1), meaning one column to the right of the Trigger cell.
You might want to do other things with your idea. I think the system I suggest can be adapted to whatever you have in mind, including changing the quantity in the Products list.
I am trying to set up a collection of spreadsheets for others to use. I am putting labels on the first row for each column, I think of them as headers in that case. And I know how to name a column in Excel (at least 2010 lets you do this).
If I have columns with the headers "higher" and "lower", and the columns have those names also, then the formula "=higher+lower" in a given row would use the values from those columns in that row to calculate the result.
I would like to end up with the descriptive column name being the same as the header value. I'd like a way to either create the headers from the column names, or create the column names from the headers, so I don't have to enter them twice. I have a lot of columns, and multiple spreadsheets to do this with; I'm just trying to save typing them all twice, and both initially and to keep them updated as they change.
Manually: Select the desired columns and go Formulas Ribbon > Create from Selection, tick "Top row" and hit OK. Repeat when you've changed a value in row 1.
With VBA: Use this code in the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("1:1"), Target) Is Nothing Then
For Each cel In Target
Debug.Print cel.Column
Columns(cel.Column).Select
Selection.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
cel.Select
Next cel
End If
End Sub
If you copy and paste the same value into multiple cells, Excel will create the first name and then prompt for the other cells if you want to replace the existing name.
Changing a cell in row 1 will create a new range name. The old range name will still remain in place.
If you have values that can be interpreted as a cell address, like A1, Excel will add a _ sign to the range name, like A1_. If you enter numbers into row 1, Excel won't create range names.
I am working on excel and do not have much experience in implementing dynamic functionality.
I am trying to program the excel sheet in such a way that it filters the values based on the value entered in the particular cell (say 4rth col and 1st row).
Col1: Client_Name
Col2: Title
Col3: Balance
So, its like a search on Col1 based on value entered by user in the 4rth col and 1st row. Is this possible using excel? I have not worked on VBA, but if that is the solution then I would like to go ahead and use it.. Any reference would help me..
I would like to have it all dynamic so that end user only has to type in the value at the cell located at 4rth col & 1st row and tab out to see the results... Other then that, the end user should not be forced to click anywhere else to get the desired results...
Just add following code to the Sheet Module corresponding the sheet with your data (see picture below). Every time value in D1 changed - filter for columns A:C would be reapplied:
Private Sub Worksheet_Change(ByVal Target As Range)
'If changed any other cell do nothing
If Target.Address <> "$D$1" Then Exit Sub
AutoFilterMode = False
'Field:=1 means that filter applies to third column of A1:C1 - i.e. column A
Range("A1:C1").AutoFilter Field:=1, Criteria1:="=" & Target
End Sub
I am trying the folowing since a few days but due to my lack of VBA skills don't get it working.
Scenario:
User: Selects a value from dropdown list (cells allow only a list
defined in another sheet).
Code: Copy the value left to the appropriate list value. (This is a list of names.)
Code: Paste the value into a specific field in sheet one.
Example:
The user is picking the value "Team One" from a dropdownlist in A1 in sheet one. This list is defined on sheet two. Next to each item of the list on sheet two is a cell with a comma separated list of names.
After the user has picked a team from the dropdown list, the corresponding list of names is copied into the field B1 in sheet one.
This procedure should only be fired when A1 is changed.
Hope I could make myself clear. If I finally find the solution myself, I will post it here.
Thank you for reading this.
You can do this without VBA. In the field you want the list of names pasted into enter this formula:
=IF(ISBLANK(<address of dropdown on Sheet1>),"",INDEX(<address of list to left of values on Sheet2>,MATCH(<address of dropdown on Sheet1>,<address of dropdown values on Sheet2>,0)))
This will be blank when nothing is selected from the dropdown and will display the appropriate list of names when a value is selected.
For example, if the dropdown is in B1 on Sheet1, the dropdown values are in B1:B9 on Sheet2, and the corresponding list of names are in A1:A9 on Sheet2, you would use this formula:
=IF(ISBLANK(Sheet1!B1),"",INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0)))
EDIT (per comment):
To use this in VBA, you'll need to do something similar to what #chris neilsen suggested. In the Worksheet module, you'll need to create a sub for a change event:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
Range("A1").Formula = "=INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0))"
If IsError(Range("A1").Value) Then
Range("A1") = ""
Else
Range("A1").Value = Range("A1")
End If
End If
End Sub
To remove any confusion, A1 is the cell that will display the output.
To do this as VBA, you would do something like the following. Per your original question, there is a list on Sheet2 that corresponds to the selection from the dropdown box and populates on Sheet1.
In the VBA editor:
In Sheet1, add the following methods
Private Sub ComboBox1_Change()
If ComboBox1.Text <> "Select" Then
Dim selVal As String
selVal = ComboBox1.Text
Range("B1").Value = GetList(selVal)
End If
End Sub
Public Function GetList(ByVal Value As String) As Variant
Dim result As Variant
result = Application.VLookup(Value, Worksheets("Sheet2").Range("A1:B100"), 2, False)
GetList = result
End Function
In the workbook object code, enter the following method:
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets("Sheet1").ComboBox1
.AddItem "Team One"
.AddItem "Team Two"
.AddItem "Team Three"
.AddItem "Team Four"
.AddItem "Team Five"
.Text = IIf(.Text = "", "Select", .Text)
End With
Worksheets("Sheet1").Activate
End Sub
I should note that you could do this without any vba by simply using a a list control found in the Data Validation option in Excel. When you make a selection change in that, you would then use a standard VLookup in cell B1 to grab the corresponding value(s).
To implement this in VBA, use a Change event to monitor the data entry cell.
Assuming you have named your validation data range as ListData, put his in the module for Sheet1
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
[B1] = Application.WorksheetFunction.VLookup(Target.Value, [ListData].Resize(, 2), 2, 0)
End If
End Sub
I would advocate an approach similar to the one that Excellll described, but with VLOOKUP rather than MATCH. To do this, you'd need to have your lists of names to the right of each team's name. For example:
| A | B
1 |Team 1 |Albert, Beth
2 |Team 2 |Carlo, Delia
3 |Team 3 |Egbert, Frederika
Now, if the team's name is at cell B7, you could use this formula to get the associated list of names:
=VLOOKUP(B7, Sheet2!$A$1:$B$3, 2, FALSE)
EDIT
See Doug Glancy's comment below explaining why Excellll's solution is better than using VLOOKUP.