How to use more than 200 nested if conditions in excel? - vba

I have the following data in excel sheet A.
Category Name
Fruit Apple
Vegetable Brinjal
XYZ Abc
I want to create a formula which takes a value for name column, outputs the corresponding category column.
If I use VLookUp, I have to copy this reference table in each and every excel sheet wherever I need to have this operation.
Hence I am looking for something similar to
IF(input="Apple","Fruit",IF(input="Brinjal","Vegetable",IF(input="Abc",XYZ,"")))
But There is limit on nested ifs in excel and no of cases that we can have in a switch case are also limited.
I have around 200 rows of this table.

use INDEX and MATCH functions. INDEX on "category" by matching "name"

You certainly don't need so many IF statements (though I note your Q Title), for example:
=CHOOSE(MATCH(D13,{"Apple","Brinjal","Abc"},0),"Fruit","Vegetable","XYZ")
which should not grow at quite the rate your version would - but with 200 'pairs' would be getting close to the limit for CHOOSE.
(D13 as example in spreadsheet.)

Related

Merging two tables while deleting duplicates in the first table and

This is my first post here and your help would be greatly appreciated! I've read a lot of other posts on this site, however I cannot find the answer to my specific question. I tried using VLOOKUP, INDEX, MATCH, Pivottables, etc. However it doesn't work out the way I want.
Background information: for my thesis I'm studying the difference in cost of capital among single segment and multi segment firms. My dataset contains two tables, one with the SIC codes of the firm's segments and one with the corresponding sales of that segment. The issue with these tables is that there are duplicate SIC codes in the first table. I want to remove the duplicates from the first table and simultaneously calculate the sum of the sales of these duplicate SIC codes in the second table.
My data looks as follows:
The SIC codes per segment and the sales per segment:
Input of SIC codes and sales per segment (one company for 20 years)
What I want to do is eliminate the duplicate SIC codes. If I change the table with SIC codes I also need the table with sales to change accordingly. However, the sales of duplicate segments should not be deleted but added to the first duplicate segment. I can computed this manually for one company, however for 1800 companies would this would be very time consuming. The manually computed output for the SIC codes and for Sales looks like this (so I don't need to merge the table, the output is still in two different tables):
Required output for the SIC codes table and Sales table (one company for 20 years
Thanks a lot!
This is the way to go about it. In 2 parts: get a list of unique SIC codes in each row, and, sum up the unique corresponding values to them.
Part I:
General logic to this part: nested INDEX functions.
Let's assume your output table (range including headers A25:J45) is exactly below your main/input table (range including headers A1:J21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
The first column should always be picking up values from the corresponding first column of input table. A26 =A2 and so on.
For B26, use this formula =INDEX($A2:$J2,MATCH(0,INDEX(COUNTIF($A26:A26,$A2:$J2),0,0),0))
You can drag this formula across to J45/end of output table. (You can use "Evaluate Formula" in excel to understand the workings of the logic)
This should populate your output table with unique SIC codes for each row.
Part II:
General logic to this part: SUM and Arrays
Let's assume your output table (range including headers L25:U45) is exactly below your main/input table (range including headers L1:U21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
In cell L26, which is the first row, left most element in the output table, you will need an array formula (Ctrl+Shift+Enter). If you don't know what array formula are read here.
Formula for L26 {=SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0))}.
You will need to enter only this in L26 =SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0)), and hit Ctrl+Shift+Enter. Excel will put the curly brackets around it on its own self.
Copy paste the formula in the rest of the output table.
Screenshot for reference based on your Excel file.
There is a remove duplicates function in excel 2010 on the Data Tab Screenshot

Excel find row of given string in range and copy to another tab

I have a column of data that looks like this:
The above is in the 'NEW PLANNER WORKBOOK' tab. Tabs are shown below:
I'm trying to find a way to get the values for the rows where "Switched" occur (in this case 13 and 16 but won't always be the same and could be more or less than 2 instances) and take the information from columns B, F, and U associated to those rows and put them into the "Switched Info" tab.
For example in the above, row 13 has the following in columns B, F, and U:
So I would like to move values 'BBMK01150', '201T1ZFPMBHFLFEDC0PMBHFLFETMD', and 'WCA' to the 'Switched Info' tab which looks like this:
The end result looking something like this:
Is there a way to do this only using formulas?
Thanks in advance for the help!
A pivot table would be best. But if a formula solution is wanted then:
These are all array formulas and must be confirmed with Ctrl-Shift-Enter.
In A4 put:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$B:$B,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In B4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$F:$F,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In C4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$U:$U,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
Again Each needs to be confirmed with Ctrl-Shift-Enter. Then they can be copied down for as many rows as desired.
One Note. This will be slow. To speed up, Change all the full column references to the specific area, ie $A:$A to $A$6:$A$1000, if 1000 was the last row of data.
You can definitely do this with code, but I think you could just use pivot tables if you didn't want to use a macro. Use the table in the New Planner Workbook worksheet as your data source and then make a pivot table on a new sheet and filter the data.

Concatenation and proportion in access

A little background:
I have two tables imported from excel. One is 300k + rows so when I do updates to it in excel it just runs too slow, and often doesn't process on my comp. Anyways, I used a 'outer' left join to bring the two together.
Now when I run the query, I get the result which works fine but I need to add some fields to these results.
I am hoping to mimic what Ive done in excel, so I can create my summary pivots in the same manner.
First, I need a field that just concatenates two others after the join.
Then I need to add a field the equivalent of:
1/Countif($T$2:$T$3330,T2) from excel to access. However, the range does not need to be fixed. I will get it so that all the text entries are at the top of the field, so in theory, i need the equivalent of Sheets("").Range("T2").End(xldown). This proportion is used to eliminate double counting when i do pivot tables.
I am probably making this much more complicated than it has to be but I am new to Access as well, so please try to explain some things in explanations.
Thanks
Edit: I currently have:
Select [Table1].*, [Table2].PlaySk, [Table2].Service
From [Table1] Left Join [Table2] On [Table1].Play + [Table1].Skill
= [Table2].PlaySk
And in a general case, what I am trying to solve is something to get ColAB and ColProportion.
ColA ColB ColAB ColProportion
a 1 a1 .5
b 1 b1 1
a 1 a1 .5
b 2 b2 .3333333
b 2 b2 .3333333
b 2 b2 .3333333
Sounds to me like you'll need to make a couple queries in sequence to do everything you need.
The first part (concatenate) is relatively easy though -- just take the two field names you wish to concatenate together, say [Play] and [Skill], and, in design view, make a new field like "PlaySk: [Play] & [Skill]".
If you want to put a character between them (I often do when I concatenate, just to keep things straight), like a semicolon for example, you can do "PlaySk: [Play] & ';' & [Skill]".
As for the second part, I think you'll want to build a "Group By" query on top of the other one. In your original query, make another field in design view like this: "T2_Counter: Iif([The field you're checking, i.e. whatever column T is] = 'whatever value you're checking for, i.e. whatever T2 is',1,0)". This will result in a column that's a 1 when the check is true, and a zero otherwise.
Then bring this query into a new one, click "Totals" at the top in the Design tab, then bring the fields you want to group by down. Then create a field in design view like this: "MagicField: 1/Sum(T2_Counter)".
Hopefully this helps get you started at least.

Excel randomly select name from list with multiple entries

I have an excel 2007 worksheet with employee names in column A and total number of entries in column B. I need to be able to randomly select x number of employee names from the total number of entries, allowing for the fact that some will have multiple entries.
For example:
Amy............30
Brian..........12
Charlene.......15
Michael.........1
Nathan..........7
What is the best way to do this?
My initial thoughts are:
1) find the max() of column B occurances of a random number in another column, like C. Then find the top values for all of that new column.
2) create a VBA array of all of the potiential entries and randomly pick one from there.
3) loop through all of the names in column A and create a temp worksheet with column B instances of each, then assign a random num generator and choose the top n.
Having said that, there may be something a lot easier. I am not sure where to begin. Normally I can find code that is similar to what I need, but I am not having any luck. Any help that you can offer would be appreciated.
Thank you in advance.
I would probably do something like this if I understand your question correctly(I just read your question title):

How do I get MS Access 2007 to calculate row percentages?

How do I write a query in MS Access 2007 that displays both the count AND the percentage of the total records in a table for each row in a specified field?
For instance, if my table has a field called "gender," what is the query I should write to see the row count and percentage of Men and the row count and percentage of Women?
So, if the table had 1000 records, the result would look something like:
Men.....600.....60%
Women...400.....40%
I can easily write a query that just gives me the row count, but I can't figure out how to also see the percentages.
I should mention that I do not know SQL. I use the Design View when creating queries. But if you can give me the text for the query in SQL, I can copy and paste it into a new query and then save it.
It's not part of SQL itself, but you can use a VBA function in a query to count the amount of records in a table: DCount (MSDN).
Copy and paste the following lines in the query designer (top row, one line per column):
TotalMen: DCount("*";"[TableNameHere]";"[Gender]='Male'")
TotalWomen: DCount("*";"[TableNameHere]";"[Gender]='Female'")
You can then get the percentage by performing some simple math on the return values (again one line per column):
TotalPersons: [TotalMen]+[TotalWomen]
PercentageMen: [TotalMen]/[TotalPersons]
PercentageWomen: [TotalWomen]/[TotalPersons]
Don't forget to set the format to Percentage or the values will show up as 0,6 and 0,4 instead of 60% and 40%.
By the way, there are probably more consise ways to do it. I tried to make it readable.
PS: don't forget to replace the names of the tables and fields.