Excel IF Statments or VBA - excel

I have a sheet that in Column M it has Date Visited and then I have put in a formula that then adds 6 months to the date which is displayed in Column N.
Then I have a hidden two columns that works out the date and and from that date it has been RAG assessed, Red Amber Green.
Column M is Date Visited
Column N is Next Visited
Column Q is the formula that tells you how many days over or under
Column R is where based on Column Q if it is G, A , R (Green, Amber or Red)
I have this formula which list all the dates and removes any blank cells in column N
=IFERROR(INDEX(Sheet2!$N$3:$N$78, SMALL(IF(LEN(Sheet2!$N$3:$N$78)=0,"", ROW(Sheet2!$N$3:$N$78)-MIN(ROW(Sheet2!$N$3:$N$78))+1), ROW(Sheet2!N2))),"")
What I want is when the below formula is true then the above to be triggered but only for cells that have R in Column R
=IF (CNI!R3="R",SHEET2!N3,"")
Hope this makes sense.
I don't mind using VBA if it is easier to achieve

Sorry, I'm a couple days behind on responding to you for this.
=IFERROR(INDEX(Sheet2!$N$3:$N$78, SMALL(_
IF(LEN(Sheet2!$N$3:$N$78)=0,"", ROW(Sheet2!$N$3:$N$78)-MIN(ROW(Sheet2!$N$3:$N$78)_
)+1), ROW(Sheet2!N2))),"")
But you want the this to happen if you have an additional criteria, this:
=IF (CNI!R3="R",SHEET2!N3,"")
You can have multiple criteria in your if statement using AND(). I am thinking that you could use the following, presuming you only want that specific If statement to apply (taken from the above, broken out If statement in the first coding section of my post):
IF(AND(CNI!R3="R",LEN(Sheet2!N$3:$N$78)=0),"", ROW(Sheet2!$N$3:$N$78)-MIN(ROW(Sheet2!$N$3:$N$78)
If your goal is to replace (from your second bit of code) the Sheet2!N3 (your true condition), then you would look at the following line, which includes both lines of your code:
=IF (CNI!R3="R",IFERROR(INDEX(Sheet2!$N$3:$N$78, SMALL(IF(LEN(Sheet2!$N$3:$N$78)=0,"", ROW(Sheet2!$N$3:$N$78)-MIN(ROW(Sheet2!$N$3:$N$78))+1), ROW(Sheet2!N2))),""),"")
Let me know if that helps clear up the issue!

Related

Need to count contents of cells to produce a knitting pattern

So this needs a bit of detail:
n,X,X,X,n is in cells B5 to F5
I need to get the following output:
1n,3x,1n
for this particular row.
Now the n's and X's represent stitches in knitting with the "n" being the background color and the "x" being the front color.
There is an array of cells B5:F12 representing the rows and stitches, so each row will have a different arrangement of stitches or background color.
I need to avoid vba as this needs to be as stable as possible with the user being my Mum who is 90 years old :) and all she needs is a place to enter the name and the layout (which I have done) and a pattern list for each row (also sorted).
I have started to consider things like:
if(B5=C5,1&B5,"")
But given the n umber of combinations that becomes very long.
Any ideas? Cheers.
You could try:
Formula in H5:
=BYROW(B5:F12,LAMBDA(x,LET(z,REDUCE(VSTACK(TAKE(x,,1),1),DROP(x,,1),LAMBDA(a,b,IF(b=#TAKE(a,,-1),IF(COLUMNS(a)=1,VSTACK(b,TAKE(a,-1)+1),HSTACK(DROP(a,,-1),VSTACK(b,DROP(TAKE(a,,-1),1)+1))),HSTACK(a,VSTACK(b,1))))),TEXTJOIN(",",,DROP(z,1)&TAKE(z,1)))))
I'll see if I can golf the bytecount down a bit...
EDIT:
After a considerable amount of golfing (came down to 119), I came up with:
=BYROW(B5:F12,LAMBDA(x,MID(REDUCE("",x,LAMBDA(a,b,IF(RIGHT(a)=b,LEFT(a,LEN(a)-2)&1+LEFT(RIGHT(a,2)),a&",1")&b)),2,99)))
Though less dynamic than the 1st one, but possible due to the fact there are only <10 columns for each knitting pattern.
If your mother doesn't have the latest Excel (with LAMBDA etc), here is an alternative to #JvdV's answer which only uses LET,SEQUENCE and FILTER.
It only accepts a single row, so you'd need to fill the formula down.
=LET(p,LOWER(B5:F5),c,COLUMNS(p),s,SEQUENCE(,c),
a,IF(s=c,c,IF(INDEX(p,,s)<>INDEX(p,s+1),s,0)),
b,FILTER(a,a>0),t,SEQUENCE(,COLUMNS(b)),
n,IF(t=1,INDEX(b,,t),INDEX(b,,t)-INDEX(b,,t-1)),
TEXTJOIN(",",TRUE,n & INDEX(p,,b)))
I might add that it allows for adding more than one colour into the pattern ...
and with a bit of conditional formatting, the good lady can design her own multicolour patterns!
This is just a start of a solution, but in cell "B6" you can put the formula:
=(IF(B5=A5,A6+1,1))
This will generate following list:
B C D E F
5: n x x x n
6: 1 1 2 3 1
From there, you can try to get the Subtotals feature to work, based on the Max formula, ... (as I said, this is just a start).
If you are willing to spread the logic over multiple sheets, it's quite easy to come up with a way to do this. Consider a workbook with three sheets:
Pattern
EqualPrevCol, where each cell of Pattern is checked for equality against the previous column of the same row.
The formula for cell EqualPrevCol!D3 is:
=Pattern!D3=Pattern!C3
And finally PatternResult, where most of the logic resides:
Consider one row of EqualPrevCol:
At every FALSE column, we want to know how many columns until the next FALSE. To do this, we want to find the next exact MATCH for D3 in the rest of the row:
=MATCH(EqualPrevCol!D3, EqualPrevCol!E3:$H3, 0)
If no match is found, that means the rest of the row is all TRUE. In this situation, we want to return the length of the rest of the row plus this current cell.
=IFNA(MATCH(...), COLUMNS(D3:$H3))
And finally, we append this to the current character:
=IFNA(...) & Pattern!D3
Also, if the 7 row at this column is TRUE, we want to keep this blank:
=IF(EqualPrevCol!D3, "", IFNA(...) & ...)
The full formula of cell PatternResult!D3 is:
=IF(EqualPrevCol!D3, "", IFNA(MATCH(EqualPrevCol!D3, EqualPrevCol!E3:$H3, 0), COLUMNS(D3:$H3)) & Pattern!D3)
Finally, the pattern is condensed to the Pattern sheet. The Pattern!B3 cell contains:
=TEXTJOIN(", ", TRUE, PatternResult!D3:$H3)
To scale this up, you simply need to change all occurrences of $H in the formulas (this was a reference to the last column) and re-fill the cells on the latter two sheets.

Condensing nested if-statements with multiple criteria

The blue columns is the data given and the red columns is what is being calculated. Then the table to the right is what I am referencing. So, F2 will be calculated by the following steps:
Look at the Machinery column (D), if the cell contains LF, select column K, otherwise select column L
Look at the Grade column (E), if the cell contains RG, select rows 4:8, otherwise select rows 9:12.
Look at the Species column (A), if the cell contains MS, select rows 5 and 10, otherwise.......
Where every the most selected cell is in columns K and L, copy into column F.
Multiply column F by column C.
I don't want to make another column for my final result. I did in the picture to show the two steps separately. So column F should be the final answer (F2 = 107.33). The reference table can be formatted differently as well.
At first, I tried using nested-if statements, but realized that I would have like 20+ if statements for all the different outcomes. I think I would want to use the SEARCH function to find weather of not the cell contains a specific piece of information. Then I would probably use some sort of combination of match, if, v-lookup, index, search, but I am not sure how to condense these.
Any suggestion?
SUMPRODUCT is the function you need. I quickly created some test data on the lines of what you shared like this:
Then I entered the below formula in cell F2
=SUMPRODUCT(($I$4:$I$9=E2)*($J$4:$J$9=LEFT(A2,FIND(" ",A2)-1))*IF(ISERROR(FIND("LF",D2,1)),$L$4:$L$9,$K$4:$K$9))
The formula may look a little scary but is indeed very simple as each sub formula checks for a condition that you would want to evaluate. So, for example,
($I$4:$I$9=E2)
is looking for rows that match GRADE of the current row in range $I$4:$I$9 and so on. The * ensures that the arrays thus returned are multiplied and only the value where all conditions are true remains.
Since some of your conditions require looking for partial content like in Species and Machine, I have used Left and Find functions within Sumproduct
This formula simply returns the value from either column K or L based on the matching conditions and you may easily extend it or add more conditions.

excel formula to give the priority to a value from a range of values in a column

This is probably a simple fix (although me thinking this, means it probably isn't), so I apologize in advance if this is mere child's play.
In an excel sheet I am working on, I have a range (for the sake of this example is B1:B10) which can contain one of 5 variables (not including blanks) - OG, D, L, PP or C.
I require a formula in another cell to review the range in question and output a value based on the following rules in this priority:
If OG appears anywhere in the column, regardless of other inputs, display OG;
If D and L and/or PP and/or C appear in the column, display OG;
If only D appears in the column, display D;
If only PP appears in the column, display PP;
If only L appears in the column, display L;
If only C appears in the column, display C; and
If all cells within the column are blank, display blank
For rules 1-6, any blank cells within the column should not be considered. It is only where all cells are blank, i.e. rule 7, that this should be considered.
I have tried IF formulas but have found these only consider a single cell.
I've tried searching everywhere and can't find anything on this (although this is probably down to me not phrasing my question/searches correctly).
Any help would be much appreciated. Thanks in advance!
Use an 'if' formula, but for your condition, Use 'MATCH': for eg, for checking the first case:
=IF(MATCH("OG",$B$1:$B$10,0), "OG", "FALSE")
The above formula will put "OG" if it's in any of the cells, else "FALSE".
In a similar way, change all your logic, but set the conditions to use match to see if they exist somewhere in the column
Here is a possible solution. For your 6 first tests, you type each of these formulas in a cell (I did it from D1 to D6) :
Test 1 (D1)
=IF(COUNTIF(B1:B10;"OG");"OG";"")
Test 2 (D2)
=IF(AND(COUNTIF(B1:B10;"D");OR(COUNTIF(B1:B10;"L");COUNTIF(B1:B10;"C");COUNTIF(B1:B10;"C";"")));"OG";"")
Test 3 (D3)
=IF(COUNTIF(B1:B10;"D")=DCOUNTA(B1:B10);"D";"")
Do the same for test 4 to 6 by replacing "D" by "P","L","C".
Now your target cell :
=IF(D7="empty";"";INDEX(D1:D7;MATCH(TRUE;INDEX((D1:D7<>"");0);0)))
This last cell will simply show the first non empty value of your test cells.
The last test is kind of implicit.
I hope I got your tests right, let me know if not.
EDIT : Sorry for the numerous editings, my Office isn't in English, that's a pain to translate the formulas. They should be working now.
EDIT 2 : Your last test is not implicit and there is a possibility that last cell show "NA". So you should add this to D7 :
=IF(COUNT(B1:B10)=0;"empty";"*A value to show if all tests didn't pass*")
Also now your target cell has D7 in its formula's range.
I feel condition 2 little confusing, Do you mean D is mandatory and other (L,PP,C) are option to result "OG" ?
What should be the result if we have multiple input of (L,PP,C) in the range ? Should it be BLANK?
I came up with this formula if D is mandatory in condition 2 and formula will return Blank if only other 3 inputs are there except "OG" and "D"
Formula: =IF(COUNTIF(B1:B10,"OG")>0,"OG",IF(AND(COUNTIF(B1:B10,"D")>0,SUM(COUNTIF(B1:B10,"D"),COUNTIF(B1:B10,"L"),COUNTIF(B1:B10,"PP"),COUNTIF(B1:B10,"C"))>1),"OG",IF(COUNTIF(B1:B10,"D")=COUNTA(B1:B10),"D",IF(COUNTIF(B1:B10,"L")=COUNTA(B1:B10),"L",IF(COUNTIF(B1:B10,"PP")=COUNTA(B1:B10),"PP",IF(COUNTIF(B1:B10,"C")=COUNTA(B1:B10),"C",""))))))
First Condition:
Condition 2:
Condition 3:
Condition 4:
Condition 5:If there is no "OG" or "D" in the array formula returns Blank.

Spreadsheet Cell Needs To Remain Blank

I have created a number of automated calculations for my trading business. However, I need to be able to reflect blank cells with no value at all.
You will notice that line 47 is completely filled and properly calculated. However, line 48 has no date or entry price. I need to have column P, Column Q, and Column S reflect blank. The calculation for each column is below, and the results can be seen in the picture.
Calc under P48 is:
=IF($D48="","",IF(OR(AND($D48="buy",$N48>=$C48),AND($D48="sell",$N48<=$C48)),"Win","Loss"))
Calc under Q48 is:
=IF(AND($P48="Win",$D48="buy"),ABS($N48-$C48)*10000,IF(AND($P48="Win",$D48="sell"),($C48-$N48)*10000,IF(AND($P48="Loss",$D48="buy"),($N48-$C48)*10000,IF(AND($P48="Loss",$D48="sell"),($C48-$N48)*10000))))
Lastly calc for s48 is:
=IF($P48="Loss",$Q48*1,"")
This is a very complicated and detailed spreadsheet. Can someone help me process these calculations to make them work properly?
Use the ISBLANK function.
Column D is not empty since it contains a zero on rows that do not have a date or entry price (like row 48), so I would use either the date column, the entry price column, or both.
In cell P48:
=IF(ISBLANK(B48),"",IF(OR(AND($D48="buy",$N48>=$C48),AND($D48="sell",$N48<=$C48)),"Win","Loss"))
Or - If either the date or the entry price is not filled in, your formula will return a blank cell with this in cell P48:
=IF(OR(ISBLANK(B48),ISBLANK(C48)),"",IF(OR(AND($D48="buy",$N48>=$C48),AND($D48="sell",$N48<=$C48)),"Win","Loss"))
Add ISBLANK to the beginning of columns Q & S formulas too.
S48:
=IF(ISBLANK(B48),"",IF($P48="Loss",$Q48*1,""))
The answer came through trial/error from the suggestions shown above. I also used File/Option/Advanced to remove all zero entries on the worksheet. In addition placed a blank condition in Q48.

excel formula multiple if criteria including date comparison

In excel, I need to check if all of the below are true:
Column G says "Void"
Column J says "No"
The date in column E is after 4/9/2014
If all are true, I need to highlight the cell in column L
Formula:
=AND(FIND("Void",G2), FIND("No",J2),$E2>=DATEVALUE("4/10/2014"))
I also tried:
=AND(FIND("Void",G2), FIND("No",J2),$E2>=DATE(2014,4,10))
Applies to:
=$L$2:$L$5000
I can get the above to work without the date check. After I put that part in, it no longer highlights the cell but no error is shown.
What do I need to change?
Im not sure what I was doing wrong yesterday but I tried the same formula again today and now it works.

Resources