Dynamically changing dropdown in excel - excel

The scenario is as follows,
I have a list of options to be filled in one cell, say A1. The options can be filled using Data Validation-List which causes a dropdown to appear in A1. Now I want B1 to contain a dropdown, the entries in which change on the basis of the data in A1.
As a contrieved example, suppose A1 offers the choices Mammals, reptiles and amphibians. If I select mammal, I should get man, cat, dog as an option in B1. If I select Reptiles, snake and lizard appear as an option in B1. If I select amphibians, I should get Frogs and toads.
I would like to avoid using VBA for this.

Use the INDIRECT formula in the validation list as is clearly explained here:
www.contextures.com/xlDataval02.html

Here you go, a solution completely without VBA. It's using actual combo box controls from the "Forms" toolbar:
Add three extra worksheets to your workbook. I called them "domain", "data" and "animal"
on sheet "domain", I did:
enter (from cell A1 downwards) "mammals", "reptiles", "amphibians"
defined a name for range "domain!$A:$A": "Domain"
defined a name for range "domain!$B:$1": "DomainChoice"
on sheet "data", I did:
enter (from cell A1 downwards) "man", "cat", "dog"
enter (from cell B1 downwards) "snake", "lizard"
enter (from cell C1 downwards) "frog", "toad"
on sheet "animal", I did:
in A1, entered the following formula
=T(INDIRECT("data!R" & ROW() & "C" & DomainChoice; FALSE))
filled this formula down to, say, row 50.
defined a name for range "animal!$A:$A": "Animal"
defined a name for range "animal!$B:$1": "AnimalChoice"
on the main worksheet, I created two combobox controls:
in box 1, I defined the properties ("Format Control...") as follows:
"Input range:" - "Domain"
"Cell link": - "DomainChoice"
in box 2, I defined the properties as follows:
"Input range:" - "Animal"
"Cell link": - "AnimalChoice"
Now should "mammals", "reptiles", "amphibians" appear in box 1, and the contents of box 2 should change based on the selection.
Look at the various sheets to see what happens behind the scenes. The only requirements are that the order of the values in the "domain" sheet corresponds to the columns on the "data" sheet, and that can only be as many animals as there are rows filled with the formula on the "animal" sheet.
You can hide the three helper worksheets, if you want.
The formula, explained:
T( // returns an empty string unless the argument is a text value
INDIRECT( // returns the value at the given reference string
"data!R" // build a dynamic reference string to "data!R1C1"
&
ROW() // the current row, changes as you fill down the formula
&
"C"
&
DomainChoice // defined name of "domain!$B:$1", contains box 1 choice
;
FALSE // flag to indicate that the formula is in "R1C1" format
)
)

Related

Setting cell that contains specific text as the first cell, or "x"

Have the below scenario;
Worksheet with a cell (A30) that contains text "Important Notes/Terms".
Macro that adds text to the row A31 (i.e. after "Important Notes/Terms") depending on a product code (with it's own notes/terms).
When I add a product as a new row, the cells shifts downwards which means "Important Notes/Terms" is no longer A30.
How do I set "Important Notes/Terms" as the first cell? E.g. if I set this first cell as "x", I want to be able to have on the macro something along the lines of
Cells(x + 1, 1).Value = "Notes/disclaimer for Product 2"
Where x is the cell that contains "Important Notes/Terms". There will only ever be one x on the sheet.
Thank you.
Just define your target cell with a Name like this
ActiveWorkbook.Names.Add Name:="MyXField", RefersTo:="=Sheet1!A30"
Now reference the location of that cell with a named reference
range("MyXField").offset(1,0).value = "Notes..."
now when lines or columns are added or deleted this named field will reference the desired location. Just don't delete the cell, the Name is lost then.

How to create a filtered drop down list based on multiple criteria

on the below Schedule image I am trying to create a Drop Down List in the "Gland (A)" Column. Rather than just creating a list of all available "Glands" I want that list to be filtered based on the data within "CORES / PAIRS", "SIZE mm" and "CABLE TYPE". For this example we will use a "3c 16 BS5467, XLPE/SWA/PVC".
Schedule
To determine the filter for the list, the "ID Ø (mm)" and "OD Ø (mm)" for the select cable need to be taken in to consideration, see Cables image below. As you can see for the example we are using the cable has an "ID" of 15.5 and "OD" of 20.35.
Cables
Finally seen below in the Glands image, the "ID" from above needs to be within the "INNER MIN/MAX" and the "OD" needs to be within the "OUTER MIN/MAX".
Glands
So back to the first image in the "GLAND (A)" columns for row 4 the drop down list should be filtered and only show concatenated values:
151/RAC/B/M25
501/453/UNIV/B/M25
ICG/653/UNIV/B/M25
In two separate formulas I managed to VLOOKUP just the "OD" based on the cable types:
=VLOOKUP(B4&C4&E4,'Cables'!A$2:H$169,8,FALSE)
Then based on the retrieved value LOOKUP the "GLAND SIZE" from within the "OUTER MIN/MAX":
=LOOKUP(2,1/((F4>='Glands'!E$3:E$9 + 1)*(F4<='Glands'!F$3:F$9 - 1)),'Glands'!B$3:B$9)
The problem is I don't know how to include checking the "ID" as well, also to retrieve concatenated cells ("GLAND TYPE" and "GLAND SIZE") and then for them to be a Data Validation Drop Down List.
Any help with this would be greatly appreciated.
Thank you
Ok, this is going to be hard to explain. I'll do my best. Maybe if we wrap this up in a dedicated sheet we won't make mistakes.
PHASE 1: create a new sheet.
Create a new sheet and name it "Calculations". We will put most of the stuff here. First of all we type "Selected row in Schedule" in the cell A1.
PHASE 2: determine what cable number is selected.
Since we have multiple entry of cable in the Schedule sheet, we will need multiple list of possible glades. Creating a dedicated list for each lane or costraining the user freedom would be unpractical. Therefore we need to know what row the user is selecting in the Schedule sheet. We have to use VBA. Right-click on the Schedule sheet name tag and click on "View code". Copy-paste this code in the window that has appeared:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Filling the cell A2 in the sheet Calculation with the row number of the selected _
cell in the scheet Schedule.
Sheets("Calculations").Range("A2").Value = ActiveCell.Row
'Preventing multiple selection in the F column of the sheet Schedule.
If Not Application.Intersect(Target, Range("F:F")) Is Nothing Then
Target.Resize(1, 1).Select
End If
End Sub
This code will report in the cell A2 of the sheet Calculation the row number actually selected in the sheet Schedule. Everytime the selection is changed, the value changes. It also prevent the selection of multiple rows of the F column in Schedule sheet (the column where Glades dropdown list will be placed). You can test the code by changing the selection in the Schedule sheet and looking at the result in Calculations sheet.
PHASE 3: determine what type of cable is selected and its ID/OD.
In the Calculation sheet, type "Selected cable" in range B1. In range B2 type this formula:
=INDEX(Schedule!$A:$F,Calculations!$A$2,2)&INDEX(Schedule!$A:$F,Calculations!$A$2,3)&INDEX(Schedule!$A:$F,Calculations!$A$2,5)
This formula reconstruct the name we will search in the LOOKUP column of the Cables sheet. It's a series of INDEX functions, nothing really complicated.
Now that we know what to look for, we can extract its ID/OD. Type "ID" in the cell C1 and "OD" in cell D1. In cell C2 type this formula:
=VLOOKUP($B$2,Cables!$A:$H,7,FALSE)
In cell D2 type this formula:
=VLOOKUP($B$2,Cables!$A:$H,8,FALSE)
These formulas will search the cables' list in the Cables sheet and extract the ID/OD of the given one.
PHASE 4: create the filtered list.
Your glands' list has its first gland in the third row. So just to make it easier to crosscheck the data, we will place our formulas accordingly. In sheet Calculations type "List stage 1" in cell E2. In cell E3 type this formula:
=IF(AND(C$2>=Glands!C3,C$2<=Glands!D3,D$2>=Glands!E3,D$2<=Glands!F3),ROW(),"")
Drag it all the way down until it will be cover the same number of rows of the glands' list in the Gland sheet. This formula will "highlight" in what rows are the glands we are looking for (if there are any). At this point the list is very long, unsorted and presumably has a lot of blank cells. We need to sort it. In cell F2 type "List stage 2". In cell F3 type this formula:
=IF.ERROR(SMALL(E:E,ROW()-ROW(F$2)),"")
Drag this one down just like the previous one. Now we have a compact list of numbers. We need to translate them into glade's names. In cell G2 type "Filtered gland list". In cell G3 type this formula:
=IF.ERRORE(INDEX(Glands!A:B,F3,1)&"/"&INDEX(Glands!A:B,F3,2),"")
Drag it down again like previously did. We have our list.
PHASE 5: name the list.
We need to create a dynamic reference to the list to cut out all the blank cells. Define a new name calling it Gland_Filtered_List referred to this formula:
=INDIRECT("Calculations!$G$3:G" & ROWS(Calculations!$G$3:$G$1048576) -COUNT.BLANK(Calculations!$G$3:$G$1048576)+2)
PHASE 6: insert data validation.
In the Schedule sheet, create a data validation for the glands column using the list mode and Gland_Filtered_List as origin.
That should do the trick. Right now i have to hurry for work, so i can't check the explanation. Everything should be in order. Try this and ask any question. I'll answer later.

How To Create Dynamic Hyperlink To Another Sheet In Excel based on the value of another cell

I have a value in the cell A1 that can be "PERSON, REGISTRATION,CONTRACT"
Then I have to add in the cell A2, an hyperlink to another spreadsheet:
If the value of cell A1 is PERSON go to spreadsheet2
If the value of cell A1 is REGISTRATION go to spreadsheet3
If thevalue of cell A1 isCONTRACT go to spreadsheet4
How can I do this please ?
Thank You -
Something like this:
=HYPERLINK("#'" & CHOOSE(MATCH(A3,{"person";"registration";"contract"},0),
"Sheet2","Sheet3","Sheet4" ) & "'!A1", "Go")
Edit: some explanation
MATCH(A3,{"person";"registration";"contract"},0) -finds the position of the value in A3 in the array of possible values.
Given that position, use CHOOSE() to pick the correct one from the three possible worksheet names to use in the hyperlink address.
The rest is just stringing all of that together to create the link formula.
Try this one:
=HYPERLINK("#"& LOOKUP(A1;{"contract";"person";"registration"};{"sheet4!";"sheet2!";"sheet3!"}) &"$a$1";"LINK to " & LOOKUP(A1;{"contract";"person";"registration"};{"sheet4!";"sheet2!";"sheet3!"}))
It is important the lookup_value must be in alphabetical order and you should be sort the lookup_vector accordingly.

Complex cross-worksheet Excel formula with string manipulation

I am generating an Excel workbook from C# code, but the code will end there as the workbook must be distributable to people who don’t have access to the VSTO stuff.
The workbook contains:
A) “Worksheet 1” that contains a dropdown list which gives “1” or “2” depending on the selected item.
B) “Worksheet 1” also has several cells in a grid layout which currently contain “1”.
C) On “Worksheet 2” there are string values in cells A1 and A2 (which relate to the drop-down items), e.g.
C01010101
C01010102
D) On “Worksheet 3” in the same locations as the Worksheet 1 cells (see B above) there are related string values which look like this: (note that these values could be any length)
checkbox|001|C010101010101
checkbox|002|C010101010102
checkbox|002|C010101010103
etc
What I need to do is add a formula to the cells at B so that rather than just containing "1" they result in a value according to the following logic:
N1 = selected index (1-2) of drop-down (in A above)
S2 = string value from “Worksheet 2”, cell range “A” & N1
S3 = the 3rd pipe delimited field value from “Worksheet 3”, from the cell which corresponds with the one in “Worksheet 1” (i.e. same address, C5, B6 etc.)
If S2 matches the left end of S3, return “1” otherwise return “0”
Finally, if possible, I would like to colour the cells so that if 0 is returned they have a grey background, but this must be unchanged if someone overtypes the cell manually with a 0.
I don't expect anyone to provide the entire solution - although of course that would be great! - but all suggestions as to how I can achieve this would be gratefully received!
[Edit]
Images attached.
Note that although the output grid is displayed with ticks, these actually contain a "1" if ticked or "0" if unticked.
It's the "1" that the formula will be replacing, because the cell should only be "valid" if the appropriate list-item is selected - if that makes sense :$
[/Edit]
[Edit2]
Ok this is my progress so far:
=IF(LEFT(MID(F17,FIND("|",F17,FIND("|",F17)+1)+1,999),LEN(E17))=E17,1,0)
For this simplified (:o) example F17 is the cell containing the pipe delimited string and E17 contains the drop-down value to match.
Now I need to work out how to reference a range using a row number read from the drop-down...
[/Edit2]
This is the final formula I came up with:
=IF(
LEFT(
MID(
Sheet2!V19,
FIND(
"|",
Sheet2!V19,
FIND(
"|",
Sheet2!V19
)+1
)+1,
999),
LEN(
INDIRECT(
"Sheet3!"&ADDRESS(T16,1)
)
)
)=INDIRECT(
"Sheet3!"&ADDRESS(T16,1)
),
1,
0
)
where Sheet2 is the sheet containing the pipe delimited strings, Sheet3 column A contains the drop-down lookup values, V19 is the current cell, T16 is the cell containing the drop-down.
As required, this causes the current cell to display 1 if the drop-down lookup matches the third word in the pipe delimited string, or 0 otherwise.
I'm going to the pub...

Excel - Selecting from cell A1 to AX, where X is read in cell B1

I have an Excel with 2 worksheets, first will be dinamically filled with data, having a row with a combo box feeding from row A on the second worksheet.
The second worksheet will also be filled dinamically, where will be:
Row A:
some values of variable number
B1 - Number of values on Row A to considerate.
My question is - Im using Data Validation > List to define the values on the ws1rowA combo box, is it possible to range from A1 to A(value in B1) ?
So far tried this on Data Validation "source" field:
=Sheet2!$A$1:offset(Sheet2!$A$1,=Sheet2!$B$1,0,1,1)
but an error is returned
You can also use INDIRECT function for this.
=INDIRECT("Sheet2!$A$1:$A"&Sheet2!$B$1)
In my version of Excel "You cannot use references to other worksheets or workbooks for Data Validation criteria" but you can use named ranges that have a Workbook Scope, so name a range (eg DataValid) to apply to say the range as #Maxim Korneev and then for Data Validation in Sheet1 use a list whose Source: is =DataValid.
Sure, just use
=OFFSET(Sheet2!$A$1,0,0,Sheet2!$B$1,1)

Resources