How to use VLOOKUP function in MS Excel - excel

I am very very new to Excel
I have two sheets
Sheet 1
Country PMU Cluster
A Asia Mercury
B Australia Venus
C North America Jupiter
All the countries and continents are unique here
In sheet 2
I have
CountryCode Country PMU Cluster
123 A
234 A
453 B
235 C
1 country can have multiple codes
I have to take the PMU and Cluster and merge it with Sheet 2 , sheet 2 will have an additional column of Country Code.
Any help is very much apprciated.

Replacing my answer per your edits.
I'm just doing this on a single sheet but you can easily adapt by pointing to your other sheet for your lookup array.
Here is the formula for cell G2:
==VLOOKUP($F2,$A:$C,2,FALSE)
Here is the formula for cell H2:
=VLOOKUP($F2,$A:$C,3,FALSE)
Drag your formulas down and you're done. Vlookup formulas are very useful I recommend looking up how they work as someone else could better explain than I. Basically, you are looking up a value (column F) in an array (columns A,B,C) and returning a column index (B = 2, C = 3, etc) for a match. Lastly, you are looking for an approximate (TRUE) or exact (FALSE) match. Almost always use FALSE.
Also, look up cell references and how to lock them (ie, how $ signs rules vary). That way you can easily drag formulas across and keep your lookup value and array the same.

Related

How to use SUMIFS and SUMPRODUCT function **with a multiplier column** for summing values if criteria range exists in a range?

This question refers to this Stack Overflow question.
This is the original question:
A B C
a 3 d
b 1 a
c 8 e
d 5
I want to use SUMIFS function, sum range is B1:B4, if the corresponding value in column A exists in column C, sum the values in column B, in this case the sum will be 3 + 5 = 8.
The answer turned out to be:
=SUMPRODUCT(SUMIF(A1:A4,C1:C3,B1:B4))
I would like to create the same result, but with a multiplier on one of the ranges.
Here's my question:
A B C D
a 3 d 3
b 1 a 1
c 8 e 1
d 5 d 3
The only thing I would like to do differently is add a range D1:D3 that acts as a multiplier. In this case, the sum I want would be (3x3)+(5x3)= 24.
One challenge is that the ranges are different sizes in the "Test Data" google sheet linked below. The "Test Data" sheet is an example of how I would like to total daily calories based on the number of servings of items that I eat as dictated by the "multiplier column."
I would like the numbers in C2:C27 to be a multiplier and factored into the total in D36.
For example, if I ate 2 servings of Beef, then 2 servings worth of Beef's ​calories would be displayed in the daily totals below the data. Currently, using the formula =SUMPRODUCT(SUMIF(Meal_Items!$A$2:$A$100,$D$2:$D$28,Meal_Items!$B$2:$B$100)), only 1 serving of beef is weighted.
Test Data
See linked Test Data set above
The original question could also be solved with this formula
=SUMPRODUCT((COUNTIF(C1:C3,A1:A4)>0)+0,B1:B4)
using that version it's a simple matter to add one or more multiplier columns
=SUMPRODUCT((COUNTIF(C1:C3,A1:A4)>0)+0,B1:B4,D1:D4)
Given discussion in comments this formula should do what you want in either Excel or google sheets
=SUMPRODUCT(IFERROR(LOOKUP($D$2:$D$28,Meal_Items!$A$2:$A$100‌​,Meal_Items!$B$2:$B$‌​100),0),$C$2:$C$28)
If you have actually put A, B, C, etc into the header row (row 1) then this will not work until you either rename your header column labels appropriately or use data range references in place of the full column references I've provided.
=B2*INDEX(B:B, MATCH(A2, A:A, 0))+B2*INDEX(B:B, MATCH(C2, A:A, 0))
In E1 enter:
=IF(COUNTIF(C$1:C$4,A1)>0,1,0)
and copy down. Then in another cell:
=SUMPRODUCT((E1:E4)*(D1:D4)*(B1:B4))
Data is in column BWeights are in column DCriteria are in column E

Excel look up value in array, return next value

I would like to look up a value in a range and return the value in the next row, but can't quite figure out how to do this. I especially would like to do this with formulas rather than VBA, and preferably with built-in formulas than custom (VBA) formulas, due to macro security issues.
I'm using Excel 2010. My workbook has two worksheets, "assessment" and "lookup". In lookup, I have lookup tables.
"lookup" looks something like:
Column A Column B Column C
1 Sales Engineering Manufacturing
2 Alice Bobbie Charlie
3 Dawn Edgar Frank
4 George Holly Isabel
In "assessment," I have some some drop downs from which users select one name from each column in "lookup." Based on some other criteria, I then rank these and create a new, sorted list (using INDEX() and MATCH()) that produce the selected name and corresponding column name a new sort order
Column A Column B
10 Engineering Edgar
11 Sales Alice
What I'd like is to return the name from the next row.
Column C
10 Holly
11 Dawn
But I'm having real trouble figuring out how to get there.
Assuming lookups is located at B2:D5 (change as required) and the result data is at F2:H3 (change as required) enter this formula in cell H2 then copy down.
=INDEX(
INDEX($B$2:$D$5,0,MATCH($F2,$B$2:$D$2,0)),
1+MATCH($G2,
INDEX($B$2:$D$5,0,MATCH($F2,$B$2:$D$2,0)),0))

Find distinct values based upon multiple columns

I have a spreadsheet of sales with (to keep the example simple) 3 columns
NAME -- STATE -- COUNTRY
It's easy to find how many sales. (sum all the lines)
I can find out how many customers I have but how about finding out how many customers from a particular state (and country)
NAME -- STATE -- COUNTRY
p1----- CA------ USA
p2----- CA------ USA
p1----- CA------ USA
p1----- CA------ USA
p3----- NY------ USA
p3----- NY------ USA
The above example would give 2 unique customers from CA and 1 unique customer from NY and 3 from the USA
EDIT:
The desired result from the above table would be
STATE - UNIQUE CUSTOMERS
CA ---- 2
NY ---- 1
COUNTRY - UNIQUE CUSTOMERS
USA ---- 3
Assuming your data have headers in row 1 of columns A, B, and C, follow these directions.
In cell F1 enter STATE.
In cell G1 enter COUNT.
In cell F2 enter this array-formula (must be confirmed with Ctrl+Shift+Enter↵):
=IFERROR(INDEX(B$2:INDEX(B:B,COUNTA(B:B)),MATCH(0,COUNTIF(F$1:F1,B$2:INDEX(B:B,COUNTA(B:B))),)),"")
In cell G2 enter this regular formula (confirmed with Enter):
=IF(LEN(F2),COUNTIF(B2:B13,F2),"")
Select F2:G2 and copy.
Now select F3:F51 and paste.
UPDATE
The nature of the question changed. The first formula is exactly the same as before. It gets the distinct states in the source data and culls them so they display with no blanks.
The second formula is now different. It needs to count the number of distinct customers in each state, and it is now an array formula confirmed with Ctrl+Shift+Enter↵).
=IF(LEN(F2),SUM(IF(F2=$B$2:$B$50,1/(COUNTIFS($B$2:$B$50,F2,$A$2:$A$50,$A$2:$A$50)),)),"")
This formula (entered as an array formula CTRL-SHIFT-ENTER) will count the number of occurrences of a Name in MyState
=COUNTIFS(Names,Names,States,MyState)
So if MyState="CA" this would return {3;1;3;3;0;0}
To get the number of names in CA you can sum the reciprocals of this array, EXCEPT taking the reciprocal of zero is invalid/infinite. So wrap the formula above in a test for zero: if it's zero, output zero, otherwise take the reciprocal (one of the rare situations where you get to set infinity equal to zero!):
=IF(COUNTIFS(Names,Names,States,MyState)=0,0,1/COUNTIFS(Names,Names,States,MyState))
(Still an array formula.)
For CA this will return {0.333333;1;0.333333;0.333333;0;0}
The final step is to sum with the array formula:
=SUM(IF(COUNTIFS(Names,Names,States,MyState)=0,0,1/COUNTIFS(Names,Names,States,MyState)))
It's possible that this could return say 2.99999... instead of 3 due to rounding errors. If that's a problem you can fix it by wrapping it with the ROUND function or setting the display format zero decimal places.
It should be straightforward to modify this to count by country. Hope that helps.
Since the question also has an 'google-spreadsheets' tag, this would be my suggested formula to use in a google spreadsheet:
For the state counts:
=query(unique(ArrayFormula({A2:A&B2:B, A2:C})), "select Col3, count(Col1) where Col3 <> '' group by Col3 label count(Col1)''",0)
And for the country counts:
=query(unique(ArrayFormula({A2:A&B2:B&C2:C, C2:C})), "select Col2, count(Col1) where Col2 <> '' group by Col2 label count(Col1)''",0)
Also see this example spreadsheet.
Easy with a PivotTable in Excel 2013:
Also easy with Google Spreadsheets:

Excel 2013: How to sum values of a given range by omitting values given in a different range

I have a WorkSheet with ProfitCenter, Cities and Amounts (called Data).
Then I have a second worksheet (same workbook) with exceptions of cities (named Exceptions)
Now I would like so sum all Profitcenter amounts in a range on Data-Worksheet except those where there cities are in a range of the exceptions-worksheet, but just for a specified report.
E.g.:
Data-Sheet:
A B C
ProfitCenter | City | Amount
111 NY 1000
111 Paris 2000
222 Paris 3000
333 Rom 500
111 London 600
111 Helsinki 8000
Exception-Sheet
A B
City | Reportname
Paris A
Paris B
London A
To demonstrate a bit better what I want to do, here is the formula that doesn't work:
=sumproduct((Data!c:c)*(Data!A:A = "111")*(Data!B:B <> Exception!A:A)*(Exception!Reportname = "A"))
The Problem is, I have no fix cell value to use for comparison but two different ranges
Is there anybody who can help me solving that Problem?
The simplest approach here seems best for your purposes. This will be by adding in a helper column in your Data sheet, which will define each cell's acceptability for summation.
In your Data sheet, on column D, add this formula (starting D2 and copied down), which does the following things: (1) Counts the number of times that the city in cell B2 on the Data sheet appears on the exception list, (2) excluding items where the Report name does NOT equal "A" [you will likely want some method of formulizing which report you care about, but I left it hardcoded for now], and (3) Checks to see whether the amount of matches on the exception list is equal to 0.
=COUNTIFS('Exceptions Sheet'!A:A,B2,'Exceptions Sheet'!B:B,"<>A")=0
Then simply have E1 sum the results, as follows:
=SUMIFS(C:C,D:D,TRUE)

SumIf with lots of data in Excel

what I'm trying to do is a simple sumif for about 200k lines of data which causes problems for excel.
Basically my list looks like this
List of Companies Dummy1 Dummy2
Company A 0 1
Company A 0 1
Company A 1 1
Company B 1 1
Company B 0 1
Company B 0 1
....
and if there is a 1 in any row of column B for a specific company I need to plug a 1 in each row of column C for this company.
So Dummy 2 is basically the sum over Dummy 1 for all entries for a specific company.
The data is already sorted by column A.
Anyway, Excel goes crazy.
Is it just plain stupid what I'm doing here because I'm generating too many comparative operations?
What would be an easy way to accomplish what I'm trying to do here?
According to your sample data, filling C2:C200000 with,
=SUMIF(A:A, A2, B:B)
... will be performing 3× as many SUMIF calculations as is necessary. An IF formula only processes the part that is TRUE or FALSE depending on how the criteria resolves so changing the formula to something like the following,
=IF(A2<>A1, SUMIF(A:A, A2, B:B), C1)
... should drastically reduce the processing in a calculation cycle. The degree of improvement will depend upon how many duplicate company values are in column A and whether column A has been sorted to keep the company names together. The smaller the number of unique companies, the more improvement you will see. In short, unless the company changes from row to row, the SUMIF is not calculated.
Sample Calculation Timing Environment:
Excel 2010 64-bit (14.0.7015.1000) running under Windows 7 Pro on a business class i5 laptop w/8Gbs DRAM.
XLSB; Calculation Manual; Recalculate workbook before saving OFF; Save AutoRecovery information OFF
Test 1: 26 companies (Company A to Company Z), each with ~7683 entries in column A, sorted. Column B random 0's and 1's reverted to values. C2:C200000 cleared, worksheet calculated then formula filled in C2:C200000 and new calculation cycle timed to completion.
formula calculation cycle (hh:mm:ss)
=SUMIF(A:A, A2, B:B) 00:21:44
=IF(A2<>A1, SUMIF(A:A, A2, B:B), C1) 00:00:09
Test 2: 5000 companies (Company 0001 to Company 5000), each with ~40 entries in column A, sorted. Column B random 0's and 1's reverted to values. C2:C200000 cleared, worksheet calculated then formula filled in C2:C200000 and new calculation cycle timed to completion.
formula calculation cycle (hh:mm:ss)
=SUMIF(A:A, A2, B:B) 00:22:10
=IF(A2<>A1, SUMIF(A:A, A2, B:B), C1) 00:00:37
      
You cannot magically break the physical laws of time and space but sometimes you can fool them. This solution may not be perfect but perhaps it is something that you can live with.
On a related note, large(r) worksheets benefit from having their formulas reverted to result values once calculations have been made if those results are not likely to change on a regular basis. While Copy, Paste Special, Values is a reasonably quick method of accomplishing this, selecting a large number of cells containing formulas and running the following sub macro is lightning quick.
sub sel_2_Value
application.enableevents = false
selection = selection.value
application.enableevents = true
end sub
If locale differences are not important (currency, dates, etc) then selection = selection.value2 is even better.
The only thing that will slow down the above operation is formulas with dependents within the range being reverted to values as they will be recalculated.
I think the better way to solve this is by ussing pivot table, you can sum Dummy1 by company and get the data as summary.
Here is an examples:
http://www.excel-easy.com/data-analysis/pivot-tables.html
enter link description here
I hope this help

Resources