Excel: Data validation source - excel

I have a list and I want to display certain data based on another cell value. Let's call it "cell A1".
In my logic, every value of "cell A1" means different list items.
The problem is when I added other values to the possible data that "cell A1" could contain, the formula becomes very long and couldn't fit in the source field!
For example, if "cell A1" possible values are Tigre, Dog, Cat, Lion, Horse, Sheep and Turkey, the condition is:
=IF(D4="Tigre";'Sheet1'!$B$2:$B$1000;IF(D4="Dog";'Sheet2'!$B$2:$B$1000;IF(D4="Cat";'Sheet3'!$B$2:$B$1000;IF(D4="Lion";'Sheet4'!$B$2:$B$1000; IF(D4=" Horse";'Sheet5'!$B$2:$B$1000;IF(D4="Sheep";'Sheet6'!$B$2:$B$1000;IF(D4=" Turkey";'Sheet7'!$B$2:$B$1000;IF(D4="Val8";'Sheet8'!$B$2:$B$1000;""))))))))))
Check this image, the source field is already full! (before the end of the condition)
have you an idea how work around this issue or how to optimise the conditional formula?
PS: I'm a french user, so I apologize of my bad english! And, I know that I need to use "SI" instead of "IF" :)
Thanks.

I think you may be after something called dependent data validation.
Look here for a step by step tutorial http://www.contextures.com/xlDataVal02.html

As I see from your example, Val1 Val2 etc. are keys to redirect to specific worksheets. You can write the "mapping" data betweens these "Vals" and sheet names somewhere (possibly in a hidden worksheet or any available/hidden columns) and use it in the validation formula using a combination of INDIRECT and VLOOKUP. Say in this example I use Columns G:H in worksheet "mapping":
' Worksheet mapping
G H
Val 1 Sheet number 1
Value 2 Sheet2
Val3 My worksheet number 3
Now your data validation formula can be like this:
=INDIRECT("'"&VLOOKUP($D$4,mapping!$G$1:$H$3,2,0) &"'!$B2:$B1000")
p.s. you can then make the column mapping!G1:G3 your data validation list for the cell $D$4 that holds the keys.
I also notice here that if the ranges are not always the same (unlike in your example where they all are B2:B1000), you can also make the mapping "complete" by including the ranges in the redirection column (column H in my example).

Related

How do I take all values in a column and change their values

There are a lot of questions on how to multiply all values by some other cell or to move all values to another cell based on some value, but what I want is to take, in the example image below:
All the values that I have selected and divide by 2. I do not want another column, I just want to change all those values in the spread sheet and divide them by 2, the values themselves should change.
I have not found an answer for this any where and I sure it is super simple. For example, in:
base_damage_mod selected column, 0.03 would become: 0.015.
The only way I know to do this is manually, and that's a lot of work ...
Whats the easiest way to do this?
The easiest way to do this is by writing a macro, like in the following example:
Sub Divide_by_2()
For Each c In Selection:
If c <> "" Then
c.Value = c.Value / 2
End If
Next c
End Sub
In order to launch this, you need to select your cells (no need to copy, or press Ctrl+C), and then launch the macro.
As far as the source code is concerned, this is pretty obvious, except for the c <> "" part: I have added this in order to avoid the value 0 being filled in in empty cells.
Is there a way to do this without VBA, without macros?
Yes, there is, but it involves you creating a new column, in there type a formula, then copy the values of that formula into again another column and remove the first two columns, in other words: it's quite Messi :-)
If column C is empty (if not, temporarily insert a column), enter a 2 there next to every used column D item (*).
Copy all of column C, and "Paste Special" onto column D using Operation>Divide.
(*) If there are too many items to manually do the "2", copy this formula down column C =IF(ISBLANK(D1),"",2) and it will add them. After this, convert column C from formulas to values by copying it and using "Paste Values" to paste it back. (Special Operations won't work on formulas)

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.

Using While loop for getting the handle of rows in a particular Column

I'm still learning VBA. I'm having a requirement in which I need to work on rows which are not empty in a particular column. I tried For loop. But considering 10 thousand entries in the Excel, there were performance issues. Can you suggest a while loop which only jumps to the rows which has value and not through all the rows?
Basically I need the handle of all the rows which are not empty.
SpecialCells is your friend here.
If you want all Cells that have Constant Values in them from Column A, then you can use Columns(1).SpecialCells(xlCellTypeConstants)
If you want all Cells that have a Formula in them from Column A, then you can use Columns(1).SpecialCells(xlCellTypeFormulas)
If you want all Cells that are Blank from Column A, then you can use Columns(1).SpecialCells(xlCellTypeBlanks)
Together, these 3 will give you all cells in the column. This means that "Non Blank" will be xlCellTypeConstants combined with xlCellTypeFormulas. Another name for "combination" is Union, which lets us stick Ranges together
Dim rngNonBlank AS Range
Set rngNonBlank = Union(Columns(1).SpecialCells(xlCellTypeConstants), _
Columns(1).SpecialCells(xlCellTypeFormulas))
Dim rngLoopThrough AS Range
For Each rngLoopThrough In rngNonBlank
'You can use rngLoopThrough.EntireRow to get the entire Row
Next rngLoopThrough

Excel - Convert String with if condition to a formula

I'd like to use something like the EVALUATE-Function in Excel for if-statements.
I've got the following issue: I'd like to use Excel to validate my data. I've got three sheets:
the real data I'd like to check. Each row represents a customer and each column some data. The columns have specific names like “age”, “name”, …
the description of the checks I’d like to perform. Each row represents one check and I’ve got 3 columns: 1 check_id – an identifier of each check; 2 check_desc – a description of the check that every normal person can understand like “Age below 18”; 3 rule – the Excel Formula as a string like If(age<18, “error”, “no error”)
the place where sheet 1 and 2 should come together. Each row should represent one customer and each column one check.
Now, if I’ve got for example check_1 “If(age<18, “error”, “no error”)” and the customer data 10 and 20, then the check for the first customer should fire and the check for the second shouldn’t.
If the data is changed, and the age is set from 10 to 18, then everything should be fine, or if the rule is changes to “If(age<21, “error”, “no error”)” then the new condition should be applied to all data.
Is something like this possible?
With the evaluate function only ‘simple’ formulas work.
Thanks in advance,
Martin
Attached you can find the
Excel-Sample File
You will definitely need some VBA here. Make a custom EVAL function:
Public Function EVAL(ByRef Rng As Range, Formula As String) As Variant
Dim RngAddress As String
RngAddress = "'" & Rng.Parent.Name & "'!" & Rng.Address(External:=False)
EVAL = Evaluate(Replace(Formula, "$", RngAddress))
End Function
Then you can easily evaluate your values with formulas passed as text ($ is for parameter):
=EVAL(A1, "IF($<21,""error"",""no error"")")
(note the escaped double quotes). But you would rather pass formula from another cell - then you can specify formula in cell with single quotes:
IF($<21,"error","no error")
=EVAL(A1, B1)
I personally would rename check_desc!B2 to "Check_1" (named range) and then refer to that. You can use the INDIRECT function as well once you've renamed your column header "Check_1" as well.
Note: the value in this case should be "18" instead of "age below 18". You can of course change the number format to "age below "0.
If the relations are changing too I would insert a table that would have uniform formulae changing in each cell when changed in one cell.
The only issue you would then face is the non-expanding nature of your table. You can use VBA for this, but maintaining formulae increases your accountability even if it would be slightly easier not to deal with nasty nested functions.

How To Reference an Excel Table Cell by Row Number or Horizontal Header Using Table Notation?

I'd like to reference a single cell in a table, from outside the table, using square-bracket sheet-formula notation.
Something like:
[MyTable[MyField] 3]
or
[MyTable[MyField] 3:3]
-to reference the 3rd row of the MyField column, or:
[MyTable[MyField] MyRow]
-to reference the MyRow row (leftmost row-header) of the MyField column.
Needs to work from outside the table, ie can't use # or #ThisRow.
Not looking for methods involving MATCH, INDEX, OFFSET, etc. Not looking for VBA methods. Just straightforward table-notation. Not looking for manually creating named ranges.
Why? Because, Tables :)
Pre-2013 Excel.
(PS, didn't there used to be a way (pre-Tables) to reference cells by row and column headers? I think it was maybe called "auto-naming", or something like that.)
heh, well this works:
=Table1[Column2] 3:3
So that's progress :)
Just awesome would be a way to reference a row by the contents of left-most column.
Thx!
You can also use index() function as in:
index(MyTable[MyField], 3)
So you get row 3 from the column MyField in table MyTable.
Reference:
https://www.ozgrid.com/forum/forum/help-forums/excel-general/116365-reference-a-single-cell-in-a-table-using-structured-referencing
We can reuse the idea of the intersection operator (i.e. a space between two references) and improve it to have the relative row number of the targeted item in the table, referred to as row_nb here:
=tbl[col] OFFSET(tbl[[#Headers],[col]],row_nb,)
or just without intersection actually (cf. comment below):
=OFFSET(tbl[[#Headers],[col]],row_nb,)
E.g. =Table1[Column2] OFFSET(Table1[[#Headers],[Column2]],2,)
This way you do not depend on the position of the table in the worksheet. Well, it yields a more complicated formula where table name tbl and column name col appear twice. Here are some comments about it:
You can of course keep the OFFSET(...) part only. The downside is that the formula will never return any error if row_nb exceeds the actual number of line items in the table. It'll return e.g. just 0 if the cells below the table are empty.
Keeping a formula that throws an error when we refer to an off-table row, we can further improve it: make it "dynamic" by letting tbl, col and row_nb be parameters:
=INDIRECT(tbl&"["&col&"]") OFFSET(INDIRECT(tbl&"[[#Headers],["&col&"]]"),row_nb,)
Assuming that we've defined tbl, col and row_nb as named ranges. Or else just use cell addresses:
=INDIRECT(A1&"["&A2&"]") OFFSET(INDIRECT(A1&"[[#Headers],["&A2&"]]"),A3,)
I'm not a big fan of INDIRECT but in this case it really comes in handy to let the formula adapt to various tables, columns and line items.
To handle tables that don't start at the first row of a worksheet, we can use the ROW() function. We can determine the first row of the data in the table with:
=ROW(myTable)
Using this and the Indirect() Function we can identify the first cell in a named column with
= myTable[myField] INDIRECT(ROW(myTable) & ":" & ROW(myTable))
The 3rd cell in that column would be:
= myTable[myField] INDIRECT(ROW(myTable)+3 & ":" & ROW(myTable)+3)

Resources