Summing up a column based on multiple Criterion - excel

Basically
Column 1 contains many names,
Column 2 contains the Unique version of those many names,
Column 3 contains either 1 or 0
I want to sum the values in Column 3 based on Column 1 matching Column 2
EG
A B C
VBA VBA 1
VBA XY 0
XY ZX 1
ZX 1
XY 1
VBA 0
XY 1
ZX 1
So I want it to produce a 4th column equal in length to Column B as follows
VBA 1
XY 3
ZX 2
I've tried a few different ways and I just cant seem to get it to work.
Any help would be much appreciated

You can use
=SUMPRODUCT(($C$1:$C$8)*($A$1:$A$8=B1))
Drag/Copy down as required. Change range as needed. See image for reference.

Anyone looking for another method aside from the above SUMIF method:
If its all on the Same sheet:
Cells(I, "C").Value = Application.CountIfs(Range("B:B"), Cells;
(I, "A").Value, Range("D:D"), "1")
If its on multiple sheets:
Sheets("SheetName").Cells(I, "C").Value =
Application.CountIfs(Sheets("SheetName").Range("B:B"), Sheets("SheetName");
.Cells(I, "A").Value, Sheets("SheetName").Range("D:D"), "1")
There is probably a neater way of doing it. You could set a variable for the sheet names rather than typing Sheets("Sheetname") over and over.

Related

Excel - sum if name is subset of another

Say I have a table like this in Excel (except the third and last column, which is what I want to obtain)
Name
Value
What I want
X
1
1
X-Y
1
2
X-Y-Z
0
2
X-V
1
2
So in column 3 I want to do a sumif which sums the column "Value" across all rows where Name is a subset of the given name in the row being looked at.
E.g. for row 2 the returned value will be 2 - because both X and X-Y is a subset of X-Y - so it sums these two rows' values.
How can I do this in a formula?
Found the solution myself.
In C2 put
=SUMPRODUCT($B$2:$B$5; --(IFERROR(IF(FIND($A$2:$A$5;A2)>0;1;0);0)=1))

How can I use a relative range in an excel function

newbie with advance excel here. :)
I'm looking for a solution how to use dynamic or relative range with the expression I came up with. I have this excel sheet with multiple columns, but I only need to check 4 columns for info. The sheet has like 1000 rows and growing, and I need to go through each row and check for blank cells in each row. Obviously dragging the cell with my formula using the mouse automatically updates the cell reference on the expression. However, I'm going to use the expression in an automation tool where it looks one row at a time and captures the value into another "array" or "collection" variable.
Col A B C D
Student1 7 8 0
Student2 2 1 2
Student3
Student4 1 0 2
Student5
Student6 8 2 4
Student7
Student8 3 1 2
I have this excel expression
=IF(AND(A2 <> "", A3<>"", A4<>""),A1,"empty")
Expected results should be like the below and placed in another Sheet (Same workbook)
Student3
Student5
Student7
Thanks in advance!

How do I convert rows to columns, and repeat the adjacent cells?

I'm sorry if the title is confusing, but a visual should hopefully help:
Here's what my sheet looks like:
A B C D
1 x y z t
2 q w e r
3 y u i o
I need to generate a separate sheet wherein:
A B C
1 x y t
2 x z t
3 q w r
4 q e r
...
Basically, I need the middle two columns of the original sheet to be transposed and the adjacent columns to it pulled into my new sheet as well (as duplicate rows).
I have the transpose working correctly, and when I pull the adjacent columns that works too. The issue is, I can't autofill the sheet. When I try to drag & autofill, instead of autofilling using row 2 from the original sheet, the new sheet will autofill using row 3 (which is the same row # in the new sheet).
Please let me know if this isn't making sense, I'll try to explain better! I'm not very well versed in the Google Spreadsheets scripts - I've tried before but they seem rather cumbersome. But I'm happy to try that as well.
I'm not sure where a transpose operation would come into play but a little conditional maths and the INDEX function¹ should suffice.
=INDEX($A$1:$D$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The documentation I've linked to is Excel but the syntax is identical for these purposes.
Addendum for more columns
By adjusting the condition for the offset after the third column, more columns can be readily accounted for.
=INDEX($A$1:$J$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)>=3,COLUMN(A:A)+1,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The above collects 10 columns from the source matrix but should be auto-adjusting for pretty much any number of columns.
¹ The INDEX function accepts parameters for both row number and column number. Although typically only one of these is used, there is no restriction against supplying both against a 2D range of cells.
Put the row_number calculation,      =(ROW(1:1)-1)/2+1 into a cell and fill down. You will receive 1, 1, 2, 2, 3, 3, etc. This supplies the repeating row number from the A1:D3 range.
Put the column_number calculation,      =IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))) into a cell and fill right and down. You will receive 1, 2, 4 for the first row and 1, 3, 4 for the second. This pattern repeats itself for subsequent rows and supplies the offset column numbers from the A1:D3 range.
You can do this with 3 single arrayformulas that are dynamic to work with any number of rows of data:
Here is an image to demonstrate:
The three formulas are:
CELL A1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!A1:A&";",2))),";"))
CELL B1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(Sheet1!B1:B&";"&Sheet1!C1:C)),";"))
CELL C1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!D1:D&";",2))),";"))

Index/Match multiple columns in Excel

I have 2 sheets. Sheet 1 is set up similarly to:
Keyword Domain Rank
A Z 1
B Z 2
C Z 3
D Y 10
E Y 15
B Y 20
And sheet 2 is set up like:
Keyword (Domain Z) (Domain Y)
A 1 -
B 2 20
C 3 -
D - 10
I'm trying to have a formula that will compare the keywords in Sheet 2 with those in Sheet 1 and then return the rank that corresponds to the correct domain (that's specified in Sheet 2 for that column). I can't get any formula I use to evaluate. I've used 2 formulas so far:
=INDEX(Raw!$H$11:$H$322, MATCH(A3,IF(Raw!$D$11:$D$322=All!$B$2,Raw!$B$11:$B$322),0))
The above formula works, to a point. The problem is that it simple pulls the domain for the first instance of the keyword found, which doesn't always match the domain in the column of sheet 2. The second formula I've tried:
=INDEX(Raw!$H$11:$H$322, MATCH(B3,MATCH($C$2,Raw!$D$11:$D$322,0)))
To make the values appear in the Sheet 2 table, use the following formula:
=SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$F$1),$C$2:$C$7)
This returns 0 for non-matches - you can either format the cells to display 0 how you want, or you can use the longer/uglier:
=IF(SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7)<>0,SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7),"-")
To calculate the rank on the first sheet based on the data from the second sheet:
=VLOOKUP(A2,$F$2:$H$5,MATCH(B2,$G$1:$H$1,0)+1,FALSE)
For sample purposes, this just put your sheet2 data in F1:H5.
This looks for the corresponding keyword and then uses match to pick the right column. I named the columns Z and Y, but if you need Domain included that can be done as well. Note that this causes an error since there is no E defined in your second table - is that the case? If so, it can be adjusted to account for no matches as follows (assuming Excel 2007):
=IFERROR(VLOOKUP(A6,$F$2:$H$5,MATCH(B6,$G$1:$H$1,0)+1,FALSE),"Rank Not Found")
You could also use PivotTable with Keywords in rows and Domain names in columns. It seems like that would do the job and be a more robust solution.

Find values from one excel sheet in another

I have a column with the values in A excel sheet 1 1 1 2 2 2 3 3 3 4 4 4.... and i have in B excel sheet another column with values 1 2 4 ...., what i want is read values from B and see if they are in A sheet, for example if value 2 is in B sheet, then write true in a new column in sheet A in front of 2, and similarly false or nothing in front of value 3.
thanks
You can use a simple VLOOKUP - For example, assuming that the content of cell A1 of sheet B is 2, and that the sheet you call A is called SheetA, you can put the following formula in cell B1:
=IF(ISERROR(VLOOKUP(A1,SheetA!A:A,1,FALSE)),"",VLOOKUP(A1,SheetA!A:A,1,FALSE))
Use the approach described here:
http://spreadsheetpage.com/index.php/tip/comparing_two_lists_with_conditional_formatting/
Key formula is this: =COUNTIF(OldList,D2)=0, which you can use within the conditional formatting context as described, or to generate your true/false indicators as you mention in your question, i.e.:
=IF(COUNTIF(OldList,D2)=0,FALSE,TRUE)
OldList is just a range, and you don't need to use a named range. But if you don't name the range, just be sure to use absolute references for the range you're searching against.
Do you want a cool formula you can use to count the number of each matching value. Try this for your original post:
=IF(SUMPRODUCT(--($A1 =Sheet1!$A:$A) > 0), "True", "False")
And this to count the values: =SUMPRODUCT(--($A1 =Sheet1!$A:$A))

Resources