I am trying to build a macro that will allow me to automatically fill out the cells in a column based on 2 criteria, which are the county and the project type, both of which are their own column names. The column to be filled out will be called "Frequency Band", which will have a static range of 12 possible values.
For example, if the County is "Bergen" and the Project Type is "2", then the Frequency Band will be "800Mhz". If it is the same county (Bergen) but the project type is "3", then the frequency band will be "1900MHz".
I currently have the list of Frequency Bands listed in another worksheet (Just call it sheet 2, cells E2 – E13), so I imagine that the outline of the VBA code would look something like:
If County(A) = Lee AND Project Type(B) = 2 Then Frequency Band(C) = 800Mhz(E3)
Else if Project Type = 3 Then Frequency Band = 1900MHz
Etc., Etc. for each county
Since I’ve never coded in VBA before and have no idea of its syntax, I don't really know where to begin with a formula like this. Could anyone help get me started?
Here is one way it would work with a formula.
In cells G1:K6 there is a table with your stored data (could also be on another sheet)
Column A you put in a country, column B you put in a project type (would be good if you validated against your source data.
Column C is your lookup function
'=INDEX($H$2:$K$6,MATCH(A2,$G$2:$G$6,0),MATCH(B2,$H$1:$K$1,0))
Index takes 3 main inputs:
the array (H2:K6) which is the actual frequency values
a row number in this array, and a column number in the array
For the row number lookup your country in the list you have (g2:g6) and it will tell you it is the x number in the array which corresponds to the row in the index array
Same thing with the column and project type.
There is a ton of information on the net about index and match if you need more information.
Highlighing is to show which values the formula has picked in my three examples below.
Related
I have 2 sheets in a workbook (Sheet1, Sheet2).
Sheet 2 contains a table (Named Table1) with 5 columns:
Takeaways
Household
Clothing
Fuel
Groceries
On sheet one, I have 2 columns:
Expense Name
Expense Total
Now, what I am trying to do is:
Set the range for the Expense Name (Range 1)
Set the range for the Expense Total (Range 2)
Compare Range 1 with the respective column in the table and only add up the values for matches
For example, in Range 1 (B6:B16):
BP
Caltex
McDonalds
KFC
In Range 2 (C6:C16):
300
400
200
150
Now, all I want to do is add up the values for the Takeaways (McDonalds, KFC) and exclude anything that DOES NOT match the criteria.
So my sum total will be all occurrences of Takeaways - provided they are listed in my table - 350 in this case.
But I cannot seem to get the formula to work.
I used these sources:
https://exceljet.net/excel-functions/excel-sumifs-function
Selecting a Specific Column of a Named Range for the SUMIF Function
and ended up with this formula:
=SUMIF($B$6:$B$16;Table1[Takeaways];C6:C16)
This source:
https://excelchamps.com/blog/sumif-sumifs-or-logic/
and ended up with this formula:
=SUM(SUMIFS(C6:C16;B6:B16;Table1[Takeaways]))
Both formulae return 0.
BUT, with BOTH of them, if I change Table1[Takeaways] to "McDonalds", then it correctly identifies every occurrence of the word "McDonalds" in Range 1.
EDIT:
I have updated the formulae above to match the images below.
This is the table that contains the references:
This table contains the data:
Formula:
Cell C4 (Next to Takeaways): =SUMIF($B$6:B$16;Table1[Takeaways];C6:C16)
Cell C5 (Next to Fuel): =SUM(SUMIFS(C6:C16;B6:B16;Table1[Fuel]))
It appears that ONLY BP is being detected in the formula.
This is a an output table when I use the formulae with a single cell reference and not a table or used range:
Formula:
Cell F4 (Next to BP): =SUMIF($B$6:B$16;"BP";C6:C16)
Cell F5 (Next to Caltex): =SUM(SUMIFS(C6:C16;B6:B16;"Caltex"))
Cell F6 (Next to McDonalds): =SUMIF($B$6:B$16;"McDonalds";C6:C16)
Cell F7 (Next to KFC): =SUM(SUMIFS(C6:C16;B6:B16;"KFC"))
If I understand correctly what you're trying to achieve, I think your setup is not right conceptually.
It looks like you're trying to track expenses, and each expense (or payee) is allocated to a category ("Takeaways", "Household" etc.). From a relational-model point of view, your second table (which defines the category for each expense/payee) should only have two columns (or variables): Expense Name and Expense Category.
The table you set up ('Sheet 2') uses the categories (i.e., possible values) as different columns (i.e., variables). But there's only variable, namely the "Expense Category", and the categories themselves are the possible values.
If you set it up like that, the problem changes: you can add a dependent column to your first table that shows the category for each payee (or "Expense Name"), using a VLOOKUP() from the second table.
You can then sum the expenses for all payees matching that category.
Note: I've created the illustration using LibreOffice Calc, so there might be some small differences, but the logic is the same.
Without seeing the data in L and K I can't give you a full answer - but likely it's to do with the way you're pulling your Array
Try something similar to this
=SUMPRODUCT(SUMIFS($L$11:$L$43,$K$11:$K$43,CHOOSE({1,2},Takeaways,"anything else you wanted to sum")))
Remember SUMIFS is for multiple criteria, so if you're only calculating one, you'll need =SUMPRODUCT(SUMIF(
The way the above works is with vertical vectors only, but changing your named ranges so the table of 2 columns is 2 named ranges instead should be okay - unless it's part of your requirements
Table 2 would become expense_Name and expense_Total etc
I was about to close this as a duplicate of my own question here but there is a bit of a difference in using a named range I think. However the logic behind this follows more or less the same approach.
Working further on my partial solution below I derived the following formula:
=SUMPRODUCT(COUNTIF(Table1[Takeaways];Range1)*Range2)
The COUNTIF() part counts the number of occurrences of the cell value in your table. Therefore make sure there are no duplicates in your table. If the value is present in the table the result of COUNTIF() will be 0. This way we create a matrix of 1's and 0's. By multiplying and the use of SUMPRODUCT() we force excel to perform matrix calculations and return the correct result.
Partial solution
I used the following formula:
=SUMPRODUCT(ISNUMBER(MATCH(Range1;Table1[Takeaways]))*Range2)
The formula does the following:
The MATCH()checks if the value in Range1 is present in your table and returns the position of the matching value in your table.
The ISNUMBER() checks if a match is found by checking if the MATCH() fucntion returned a number
Multiplying this with Range2 forces matrix calculation, using the SUMPRODUCT() function
EDIT:
This worked for a really limited sample. As soon as I added the fourth row to my data the formula stopped working as intended. See screenshot:
It took the first two values into the sum correctly, the fourth is not taken into account.
At work we have a large collection of sheet that I'm trying to add a couple columns to.
Some of the Index functions were already in the sheets so I am trying to base my new index function off of it because I have never used these types of functions in Excel. The old function is as follows.
=INDEX('Order Detail'!$D:$D,MATCH($A9,'Order Detail'!$B:$B,0))
Order detail is the name of the sheet that is holding the date for the output.
A9 is the part number in the current sheet.
And the part numbers are listed in column B or the order detail sheet.
What I know want to do is match the part numbers for the two sheets and match the dept number. The dept number is located in column L of the order detail sheet and I want that to match 1000-1. Not 1000 minus 1 actual 1000-1.
IF both of those match I want to spit out the data from column O in the order detail sheet and output that in column U starting at U9 and down in the current sheet.
This is what I have so far for this but it does not work.
=INDEX('Job Routing'!$O:$O,MATCH($A9,'Job Routing'!$B:$B,0),MATCH('Job Routing'!$L:$L,"1000-1"),0)
I only want it to output the O column if the Part Number AND Dept Number are the same as the part number in A9 and the dept number the same as 1000-1.
Any ideas?
Thanks
I have a table with 2600+ rows, related to towns in my region and their population; each town has 11 rows, one for each age class (0-9, 10-19, and so on).
I need to get the sum of the population of each town; of course I can do it manually but it's a never ending job; I wonder if there's some kind of command that tells excel to do the sum every 11 rows and do it for all the towns.
I think it's a kind of loop but I have no idea about how to do it.
The problem can be reduced by using the SUMIF function. The question then becomes how to apply this to your dataset.
Assuming one of the columns in your 2600+ rows contains the town name (or another unique identifier), and you have a list of towns (or other unique identifier), the below method can be used.
The formula in E2 is =SUMIF(A:A,D2,C:C), and in E3 =SUMIF(A:A,D3,C:C). A to C is the list of all data, D is a list of towns.
For a VBA solution, you should be able to use a step in the loop.
So if you wish to step by 11 rows at a time.
Public Sub IterateRows()
Dim rData As Range, rPtr As Range
Dim dSum As Double
Dim i As long
Set rData = Sheet1.Range("A1:A1000")
For i = 1 To rData.Rows.Count Step 11
Set rPtr = rData(1).Resize(11).Offset(i - 1)
dSum = Application.WorksheetFunction.Sum(rPtr)
Next
End Sub
If you want a worksheet function solution, you will probably have to use the MOD operator and check for when the value is zero..
You can also try this manual method which is not a never ending job; i.e.:
in E11 put your formula as =SUM(C1:C11)
copy range E1:E11
select range E12:E2600 and paste special function
Do you have any reference columns? As in...say for example Column A has the Town Name, and Column B has the Age Class, and Column C has the values.
Going down the rows Column A will have repeating town names, yes?
Like this:
Town - Age Class - Pop
Wherever - 0-9 - 1000
Wherever - 10-19 - 2000
Wheverer - 20-29 - 2500
Assuming you have maintained the data structure (NO GAPS) a possible solution in Column D (or whatever column just make sure you change the references) could be (putting this in D2 and dragging it down the length of your sheet):
=IF(A1<>A2,SUM(INDIRECT("C"&ROW(A2)&":C"&SUMPRODUCT(MAX((A:A=A2)*(ROW(A:A)))))),D1)
This works if you have any amount of rows per town, so long as you SORT by town name so the same names are next to each other in the list and there are no gaps.
In the above test data set I subtracted 250 from each Values count per Town going down (each class has 250 less than the previous city) just to show some variation in the output...you can see each city has 2750 (250 * 11) less pop than the previous.
Basically it builds an array with a starting position of "not the town above" in the first row it encounters a new town name to an ending position of "last (max) position of new town in same list" so that is how it doesn't matter how many rows you have per town. From 1 to memory limit basically, I think. :)
ALTHOUGH, this also works:
=SUMIF(A:A,A2,C:C)
Yep. Not kidding just drag that down Column D...
Assuming the following structure
This is a very easy task using a pivot table.
For LibreOffice Calc:
Just select the complete data area including the column headers (in my example: A1:C13);
Menu Data -> Pivot Table;
Current selection;
Following settings for Pivot table:
(drag the Town field into the Row Fiels area, and the Count field into the Data fields area. LO Calc will offer to calculate the Sum of the count entries by default).
Hit OK - the resulting pivot table will look like this:
This solution has the advantage that the source data area hasn't to be sorted by town, and it doesn't matter if some towns don't have nine value rows each. Additionally, you don't need any formulas.
EDIT:
You can work with the contents of the pivot table the same way as with calculated results. For example, you could use the pivot table values to calculate the sum for some of the towns (in my example, calculate the sum for town B and C based on the pivot table values B3 and B4 respectively):
You could do this with the MOD function, which gives the remainder of division. You could look at each row number and if its MOD of 11 equals zero, then it's the row you're looking for.
I am counting 10 items in each section in your example so I'm not sure I completely understand. Let's assume you need to sum every row that ends in a 9 (A9, A19, A29, etc.). You can replace the 9's below with an 11.
=ROW(A9) gets you the row number.
=MOD(ROW(A9),9) gets you a TRUE or false on weather that number is divisible by 9. If it is a multiple of 9, it will return the number 0.
Now use the SUM function and hit CONTROL+SHIFT+ENTER to complete it. Note that the formula bar indicates that this is an array function by using curly braces. You don't need to type those in yourself.
{=SUM(A1:A9*(MOD(ROW(A1:A9),9)=0))}
This is a confusing request.
I have an excel tab with a lot of data, for now I'll focus on 3 points of that data.
Team
Quarter
Task Name
In one tab I have a long list of this data displaying all the tasks for all the teams and what Quarter they will be on.
I WANT to load another tab, and take that data (from the original tab) and insert it into a non-list format. So I would have Quarters 1,2,3,4 as columns going across the screen, and Team Groups going down. I want each "task" that is labeled as Q1 to know to list in the Q1 section of that Teams "Block"
So something like this: "If Column A=TeamA,AND Quarter=Q1, then insert Task Name ... here."
Basically, if the formula = true, I want to print a list of those items within that team section of the excel document.
I'd like to be able to add/move things around at the data level, and have things automatically shift in the Display tab. I honestly have no idea where to start.
If there is never a possibility that there could be more that 1 task for a given team and quarter, then you can use a formula solution.
Given a data setup like this (in a sheet named 'Sheet1'):
And expected results like this (in a different sheet):
The formula in cell B2 and copied over and down is:
=IFERROR(INDEX(Sheet1!$C$2:$C$7,MATCH(1,INDEX((Sheet1!$A$2:$A$7=$A2)*(Sheet1!$B$2:$B$7=B$1),),0)),"")
I came across this situation. When I have to insert the values into a table from an Excel sheet I need all information in 1 Column instead of 2 multiple rows. In Excel my Data looks like:
ProductID----OrderID
9353510---- 1212259
9650934---- 1381676
9572474---- 1381677
9632365---- 1374217
9353182---- 1212260
9353182---- 1219361
9353182---- 1212815
9353513---- 1130308
9353320---- 1130288
9360957---- 1187479
9353077---- 1104558
9353077---- 1130926
9353124---- 1300853
I wanted single row for each product in shape of
(ProductID,'OrdersIDn1,OrderIDn2,.....')
For quick solution I fix it with a third column ColumnC to number the Sale of Product
=IF(A2<>A1,1,IF(A2=A1,C1+1,1))
and fourth Column D as a placeholder to concatenate with previous row value of same product:
=IF(A2=A1,D1+","&TEXT(B2,"########"),TEXT(B2,"########"))
Then Column E is the final column I required to hide/blank out duplicate row values and keep only the correct one:
=IF(A2<>A3,"("&A2&",'"&D2&"'),","")
Final Output required is only from Column E
ProductID Order Id Sno PlaceHolder Required Column
9353510 1212259 1 1212259 (9353510,'1212259'),
9650934 1381676 1 1381676 (9650934,'1381676'),
9572474 1381677 1 1381677 (9572474,'1381677'),
9632365 1374217 1 1374217 (9632365,'1374217'),
9353182 1212260 1 1212260
9353182 1219361 2 1212260,1219361
9353182 1212815 3 1212260,1219361,1212815 (9353182,'1212260,1219361,1212815'),
9353513 1130308 1 1130308 (9353513,'1130308'),
9353320 1130288 1 1130288 (9353320,'1130288'),
9360957 1187479 1 1187479 (9360957,'1187479'),
9353077 1104558 1 1104558
9353077 1130926 2 1104558,1130926 (9353077,'1104558,1130926')
You will notice that final values are only with the Maximum Number of ProductSno which I need to avoid duplication ..
In Your case Product could be Team and Order could be Quarter and Output could be
(Team,Q1,Q2,....),
Based on my understanding of your summary above, you want to put non-numerical data into a grid of teams and quarters.
The offset worksheet function will work well for this in conjunction with the match or vlookup functions. I have often done this task by doing the following steps.
In my data table, I have to concatenate the Team and quarter columns so I have a unique lookup value at the leftmost column of your table (Note: you can eventually hide this for ease of reading).
Note: You will want to name the input range for best formula management. Ideally use an Excel Table (2007 or greater) or create a dynamically named range with the offset and CountA functions working together (http://tinyurl.com/yfhfsal)
First, VLOOKUP arguments are VLOOKUP(Lookup_Value,Table_Array,Col_Index_num,[Range Lookup]) See http://tinyurl.com/22t64x7
In the first cell of your output area you would have a VLOOKUP formula that would look like this
=Vlookup(TeamName&Quarter,Input_List,Column#_Where_Tasks_Are,False)
The Lookup value should be referencing cells where you have the team names and quarter names listed down the sides and across the top. The input list is from the sheet you have the data stored. The number three represents the column number the tasks are listed in your source data, and the False tells the function it will only use an exact match in your putput.
Basically my problem is that I have a string in one cell in excel, I then need to see if that string exists in another row (not one cell but the whole row) and if so then print the contents of another cell in the same row but in another column.
I will give a basic example:
Title Answer
Police 15
Ambulance 20
Fire 89
Now I need to scan the title column for, say, "Police" and then populate the cell with the value under Answer (in this case 15).
I cant just say IF(A2="Police";B2;"" as I need the scan the whole of the Title column.
I have tried using IF(COUNTIF(A$2:A$100;"Police"); which scans the contents of A2 to A100 for the string Police, and know how to make it print a constant (just put something after the ;) but cant work out how to make that "constant" a variable that changes depending on the found row. So if the COUNTIF found Police in cell A44 then the answer to my formula would be B44, the same as if it found Police in A62 then my formula should show B62
I hope this makes sense and that someone can help me :)
Note that I am using excel 2010 and need a normal formula as I can not use scripting for this document.
EDIT:
Here is what I have so far, note that the spreadsheet I am using is far more complex than the "simple" example I have in the question...
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
This is showing "RuhrP" in every answer where "RuhrP" is found in F9 and not the answer I want which should be that found in RuhrPumpen!I$5:I$100 where the cell index is the same as that for the A coloum where A9 was found. Again, sorry for the complexity I cant think of any better way to word it.
I note you suggested this formula
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
.....but LOOKUP isn't appropriate here because I assume you want an exact match (LOOKUP won't guarantee that and also data in lookup range has to be sorted), so VLOOKUP or INDEX/MATCH would be better....and you can also use IFERROR to avoid the IF function, i.e
=IFERROR(VLOOKUP(A9;Ruhrpumpen!A$5:Z$100;9;0);"")
Note: VLOOKUP always looks up the lookup value (A9) in the first column of the "table array" and returns a value from the nth column of the "table array" where n is defined by col_index_num, in this case 9
INDEX/MATCH is sometimes more flexible because you can explicitly define the lookup column and the return column (and return column can be to the left of the lookup column which can't be the case in VLOOKUP), so that would look like this:
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH(A9;Ruhrpumpen!A$5:A$100;0));"")
INDEX/MATCH also allows you to more easily return multiple values from different columns, e.g. by using $ signs in front of A9 and the lookup range Ruhrpumpen!A$5:A$100, i.e.
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH($A9;Ruhrpumpen!$A$5:$A$100;0));"")
this version can be dragged across to get successive values from column I, column J, column K etc.....
Assuming
source data range is A1:B100.
query cell is D1 (here you will input Police or Fire).
result cell is E1
Formula in E1 = VLOOKUP(D1, A1:B100, 2, FALSE)
I figured out such data design:
Main sheet:
Column A: Pump codes (numbers)
Column B: formula showing a corresponding row in sheet 'Ruhrpumpen'
=ROW(Pump_codes)+MATCH(A2;Ruhrpumpen!$I$5:$I$100;0)
Formulae have ";" instead of ",", it should be also German notation. If not, pleace replace.
Column C: formula showing data in 'Ruhrpumpen' column A from a row found by formula in col B
=INDIRECT("Ruhrpumpen!A"&$B2)
Column D: formula showing data in 'Ruhrpumpen' column B from a row found by formula in col B:
=INDIRECT("Ruhrpumpen!B"&$B2)
Sheet 'Ruhrpumpen':
Column A: some data about a certain pump
Column B: some more data
Column I: pump codes. Beginning of the list includes defined name 'Pump_codes' used by the formula in column B of the main sheet.
Spreadsheet example: http://www.bumpclub.ee/~jyri_r/Excel/Data_from_other_sheet_by_code_row.xls
Guys Its very interesting to know that many of us face the problem of replication of lookup value while using the Vlookup/Index with Match or Hlookup.... If we have duplicate value in a cell we all know, Vlookup will pick up against the first item would be matching in loopkup array....So here is solution for you all...
e.g.
in Column A we have field called company....
Column A Column B Column C
Company_Name Value
Monster 25000
Naukri 30000
WNS 80000
American Express 40000
Bank of America 50000
Alcatel Lucent 35000
Google 75000
Microsoft 60000
Monster 35000
Bank of America 15000
Now if you lookup the above dataset, you would see the duplicity is in Company Name at Row No# 10 & 11. So if you put the vlookup, the data will be picking up which comes first..But if you use the below formula, you can make your lookup value Unique and can pick any data easily without having any dispute or facing any problem
Put the formula in C2.........A2&"_"&COUNTIF(A2:$A$2,A2)..........Result will be Monster_1 for first line item and for row no 10 & 11.....Monster_2, Bank of America_2 respectively....Here you go now you have the unique value so now you can pick any data easily now..
Cheers!!!
Anil Dhawan