I want to apply the result of an IF statement on multiple cells, so for example A2, A2 and A3 are equal to 1, 2 or 3.
When A2 is equal to 1 I want to get B2,B3,B4 = 1,0,0
When A3 is equal to 2 to get B5,B6,B7 = 0,1,0
And when A4 is equal to 3 to get B8,B9,B10 = 0,0,1
Is this possible?
First thing about Excel: you can't write a formula that changes the contents of another cell. A formula only gives a result in the current cell. That result may well be based on the contents of several other cells, and the result in the current cell may be used by several other cells.
So here are two possible answers:
(1) In B2, write
=If(A2=1,1,"")
In B3, write
=If(A2=1,0,"")
and keep going for the other 7 cells B4:B10.
(2) If you wanted a single formula that would do roughly what you describe, it would be quite complicated but do-able.
In B2, write
=IF(INDEX(A$2:A$4,(ROW()+1)/3)=INT((ROW()+1)/3),IF(MOD(ROW()+1,3)+1=INDEX(A$2:A$4,(ROW()+1)/3),1,0),"")
and pull or copy this down through cells B3:B10.
Then use Formulas | Evaluate Formula to see what it does.
EDIT
If A2, A3 etc can be 1, 2 or 3 then the formula would be simpler
=IF(MOD(ROW()+1,3)+1=INDEX(A$2:A$4,(ROW()+1)/3),1,0)
you're just seeing if the remainder on dividing the row in column B by 3 (+1) is equal to the corresponding value (1, 2 or 3) in A2, A3 etc.
Related
I'm trying to:
In cell C3, search Column J to find an identical match of B3, then copy the data from Lx.
In cell D3, Search Column J to find an identical match of B3, then copy the data from Mx.
There are 10285 rows of data.
I have tried several IF and VLOOKUP statements.
In the group of columns, J:L, L is the fourth column, so use 4 in VLOOKUP:
Formula for C3:
=VLOOKUP($B3,$J:L,3,FALSE)
D3 is the same, but with 4 instead of 3 to get M.
If you want to get fancy and have the formula copy nicely across columns, you can use the COLUMN() function to help calculate the 3s and 4s:
Formula for C3:
=VLOOKUP($B3,$J:L,COLUMN(L3)-COLUMN($J3)+1,FALSE)
Then when you copy that from C3 to D3, the L becomes M and the column calculation produces 4 instead of 3.
I need to make a sum of 12 rows every 3 rows in excel. That is, I need to sum first from C4 to C15, then from C7 to C18, and so on.
You can use OFFSET function for this, also volatile, but shorter!
Assuming first formula in E2 copied down
=SUM(OFFSET(C$4,(ROWS(E$2:E2)-1)*3,0,12))
I prefer this because it explicitly contains all the required information
C4 = first cell to sum,
E2 = first cell with formula,
3 = row increment,
12 = number of cells to sum
The above gives you the sums on successive rows from E2 (or any other chosen cell) down. If you actually want the sum to be shown every 3 cells e.g. on the first row for each sum then that's simpler - try this formula in D4 copied down
=IF(MOD(ROWS(E$2:E2),3)=1,SUM(C4:C15),"")
.......or even easier.....just put this formula in D4
=SUM(C4:C15)
....leave D5 and D6 blank, then select the range D4:D6 and drag down
You can also use the non-volatile INDEX function
=SUM(INDEX(C:C,ROWS($1:1)*3+1):INDEX(C:C,ROWS($1:1)*3+12))
This works because INDEX returns a reference so you can use the normal Ref1:Ref2 notation for a range.
=SUM(INDIRECT("C"&ROW(1:1)*3+1&":C"&ROW(1:1)*3+12))
Be warned that INDIRECT() is a volatile formula... This means that any change made anywhere in the workbook this formula will recalculate and can cause performance issues.
I am attempting to compare the numerical value in one column with that in another. Then have another column populated depending on the results. I do not know how to write this due to limited experience with Excel. This is what I am attempting to do:
If B1 < C1 then D1 = C1
If B1 > C1 then D1 = B1
You should put your logic(formula) in the cell you want to give a custom behavior.
As I can see you want to put a dynamic value into column D cells (D1).
So, you should put a formula in cell D1 to behave as you want:
=IF(B1<C1,C1,B1) (sometimes you have to use ; instead of , depending on language)
It says:
If b1<c1 then put C1 into this cell otherwise B1
=IF( Logic, Case-true, Case-False )
In excel the IF operator receives 3 inputs separated by ; sign:
The logic condition in the first option
the result you want to use if condition is true
the result you want to use if condition is false
To use this formula in all lines of column D, just press the black little square in the right corner bottom of the cell and grab it until the line you want
Say I have numbers in A1 to A20 and I wanted to sum non-overlapping ranges of 5 cells in column A and store the results in cells in column E, it would look something like this (if the formulas were typed into each E column cell manually):
E1 = SUM(A1:A5)
E2 = SUM(A6:A10)
E3 = SUM(A11:A15)
E4 = SUM(A16:A20)
However, I don't want to type the formulas into E cells manually. I want to be able to select the formula in E1 and drag it down to E4, whilst maintain the non-overlapping ranges of 5 cells in A! Excel does not give me this behavior by default, it does this instead:
E1 = SUM(A1:A5)
E2 = SUM(A2:A6)
E2 = SUM(A3:A7)
E4 = SUM(A4:A8)
See how the ranges of 5 cells in each SUM() function overlap? e.g. A1:A5 and A2:A6. That's not what I want.
So, what is a formula that would enable me to do this? Basically, the following pseudocode would work, but I can't seem to implement anything like it in Excel:
SUM(CELL(COLUMN, (CURRENT_ROW - 1) * 5 + 1):CELL(COLUMN, (CURRENT_ROW - 1) * 5 + 5))
For example, for E2, CURRENT_ROW = 2, meaning it would look like this:
SUM(CELL(A, (2 - 1) * 5 + 1):CELL(A, (2 - 1) * 5 + 5))
= SUM(CELL(A, 6):CELL(A, 10))
This pseudocode assumes CELL has the method signature CELL(row, column).
The key to this is the OFFSET function. Offset takes a starting cell, and a number of rows, columns, and optional height and width to generate a reference to a cell/range on the fly. The trick would be to use other functions to generate the row offset and other parameters.
I've just knocked together something that seems to do what you want, I'll see if I can explain it...
Column A: integers (A1) 0, (A2) 1, (A3) 2,3,4,5 etc... this is an index for how many you're offsetting by, you could probably remove this using something like the ROW function)
Column D (or any other arbitrary location): the numbers you're wanting to sum, starting at D1 - I actually just used (D1) 1, (D2) 2,3,4,5...
Column B is the one that's interesting to you, formula is:
=SUM(OFFSET(D$1,A1*5,0,5,1))
What it's doing: summing the range defined by: A block of cells starting (A1 * 5) cells down and 0 across from $D$1, which is 5 high by 1 wide. See edit, I left this in because you could put arbitrary numbers in the A cells and use this principle.
Hope that makes some kind of sense? Excel doesn't lend itself to text explanations!
Edit: Removed the cells A1..., remembered that ROW() allows self-references, which means you can do
=SUM(OFFSET(D$1,(ROW(B1)-1)*5,0,5,1))
Where this formula is in Cell B1 (pasted down), and your data is in Column D starting at 1.
This formula will get correct results when entered into cell E1 and populated down into E2, E3 etc...
=SUM(OFFSET(A1,ROW(A1)*4-4,0,5,1))
I'm doing a hlookup against a value that spans multiple columns. My data is similar to this:
A B C D
---------------------------
1| Col1 Col2
2| x y z w
3|
4|
In rows 3 and 4 (A3, B3, C3, D3, etc.), I'd like to put formulas that will do an hlookup somewhere else in the workbook. The trick is, I'd like it to look up "Col1" for columns A and B and "Col2" for columns C and D. "Col1" is in A1, but is really A1 and B1 merged. When I reference A1, "Col1" appears, but when I reference B1, the return value is blank.
Any ideas?
Here is another solution that can also work when the merged cells are of different widths, let me illustrate with an example:
Open a fresh Excel, merge B1, C1, D1
Type Col1 in the merged cell
In B2, type formula =B1, and in C2 =C1, in D2 =D1
You should see B2 to be Col1 while C2, D2 are 0
In B3, type the formula =A3, copy it
Right-click the merged cell B1:D1, select "paste special -> formulas"
You should see the merged cell being 0
Type Col1 in the merged cell
You should now see all B2, C2, D2 to be Col1, i.e. now you can reference the merged cell as you expect it to be.
If you can multiple merged cells, each of different widths, just paste the formula to all of them in one go.
The reason behind this works is because of a perculier design choice by Microsoft. It seems that when you paste formulas in merged cells, each underlying cell receives the formula (in contrast, if you enter a value, only the top-left cell gets it) So you can use it at your advantage and paste a formula that reference the cell next to it, and then overwrite the top-left cell with the value you want, then every cell underlying the merged cell will have that value.
To get access to the "Col1" and "Col2" labels, you can use the following:
=INDEX($1:$1,1,COLUMN()-MOD(COLUMN()-1,2))
Note: This assumes that you are grouping together the same number of cells. If it were three cells, you would just change the last number in the formula to a 3, and so on.
Edit: Here's how it works:
INDEX($1:$1,1, x ) returns the value of the cell in row 1, column x. If your table is not actually located in the top left corner of the worksheet, you can change this to the actual range that includes all of your merged labels. In this case, it would be:
INDEX($A$1:$D$1,1, x )
COLUMN() returns the column number of the current cell (1 in column A, 2 in column B, etc.)
MOD(COLUMN()-1,x) returns an offset from the current column to the column that holds the proper label
I've built a simple function in vba that will solve this problem:
Function mergedText(rngMergedCell As Range)
If rngMergedCell.MergeCells = True Then
mergedText = rngMergedCell.MergeArea(1, 1)
Else
mergedText = rngMergedCell
End If
End Function
If the cell is a merged cell, the function will return the value in the first element of the merged cell - this is where the merged cell stores its value
A more generic variant of e.James's proposal is :
={INDEX($A$1:A1, 1, MAX(NOT(ISBLANK($A$1:A1))*COLUMN($A$1:A1)-COLUMN($A$1)+1))}
This relies on the fact that the merged cells are empty except for the first one (unless you are in a case like Martin's proposal).
Note: The curly braces are there to mark an array formula (do not enter them, just press alt+return to validate the formula in the cell).
I realize I am late to this thread but I found a really simple answer to this.
If, for example, your label is merged across 4 columns a1:d1, and if you reference b1, you will return "". For dynamically finding the right labels use this fx in your new table:
=if(OriginalTable!B1="",ThisTable!A1,OriginalTable!B1)
I am sure you will realize that this will capture ranges in e1:h1 etc as you drag across.
That's it. Hope it helps someone.
Cells B1 and D2 contain no values, only A1 and C1 have something inside them.
So you'll just have to make sure that your formulas in columns A and B both refer to A1 as the lookup value, and that your formulas in columns C and D both refer to C1 for the lookup value.
With the new dynamic reference, there are more options now. Here is a generic function I wrote that will search to the left of the cell and return the first value. It is NOT optimized, but it does the job for me.
=LET(
TargetCell, A1,
TargetRow, ROW(TargetCell),
TargetCol, COLUMN(TargetCell),
RowReference, INDIRECT(TargetRow & ":" & TargetRow),
RowValues, TRANSPOSE(FILTER(RowReference,ISBLANK(RowReference)=FALSE)),
RowValueColumns, MATCH(RowValues, RowReference,0),
ReturnColumn, MAX(FILTER(RowValueColumns,RowValueColumns<=TargetCol)),
Return, INDIRECT(ADDRESS(TargetRow,ReturnColumn)),
Return
)