IF, AND and OR statement - excel-formula

I have an Excel query around an IF, AND and OR statement. I have spreadsheet based around city center stores being open when they are not supposed to be.
I have shown a snippet of the spreadsheet and in plain English I need to do the following:
IF E2 is "Open" AND F2 is "YES" then status should read "Normal"
IF E2 is "Open" AND F2 is "NO" then status should read "Warning, this store should be open"
IF E2 is "Closed" AND F2 is "YES" then status should read "Warning, this store should not be open"
I have had a go at it and have come up with 2 AND statements (Shown below) which work independently. I have been reading up about nested statements and it looks like this is what I need to do here, however I just can`t get my head around how I do actually stitch all of it together in to 1 statement.
=IF(AND(E2="Open",H2="NO"),"Warning, this store should be open","Normal")
=IF(AND(E2="Closed",H2="YES"),"Warning, this store should not be open","Normal")

You've mentioned using AND in your formula, but I think this is easily achieved with nested IFs:
=IF(E2="Open",IF(F2="YES","Normal","Warning, this store should be open"),IF(F2="YES","Warning, this store should not be open",""))
How to visualise what the above statement does:
E2 F2 Output
Open YES Normal
Open (anything else) Warning, this store should be open
(anything else) YES Warning, this store should not be open
(anything else) (anything else) (empty string)
It might be that you want other behaviour dependent upon other values of E2 and F2, but given your requirement the above will work.

Related

Returning a column reference from MATCH to avoid using INDIRECT with a Named Range

TL;DR: I'm basically trying to obtain a column range such as 'Sheet 1'!$A:$A where the A is obtained by matching the contents of a given cell to a 1:1 range within a sheet referenced by another given cell, for use in a dynamic range.
In the highly probable case where that made zero sense, here's an illustration:
PARAMETERS: A2 = "LIST" | C2 = "FirstName" | Desired result: 'LIST'!$A:$A
And I've obtained that, BUT, I can't use that output ('LIST'!$A:$A) within formulas (namely to create a dynamic range). For instance, here 'LIST'!$A:$A contains 101 cells with values in them:
V3 = NamedFormula = 'LIST'!$A:$A
COUNTA(INDIRECT(V3)) = 101
COUNTA(INDIRECT(NamedFormula)) = 1 because it evaluates to #VALUE and that is a singular result.
Before delving into the topic of using INDIRECT with a Named Range (which I've read about and am still getting over my confused grief), I'm realizing my Names are getting a bit out of hand. I tend to use Excel like a mad scientist. So, in case there's a much simpler solution to what I'm trying to do, here's my actual mission:
0. I'm building a tool to simplify a process where email addresses are built from different data, which needs to run without any scripts, only formulas.
1. A tab with no imposed name would contain a user database with minimally (firstname and lastname OR IDs) AND (potentially other data columns) in no specific order. Tool users would import that tab from wherever the data got to them depending on the client, and would only need to copy-paste relevant headers to the main tab without changing anything else here for data integrity.
2. The main tab would have specific input fields where tool users would paste in the name of the imported tab as well as the labels of the columns they need (for instance, the labels in the first row of the columns containing the first name and the last name), and an input field for the domain name to use to build those email addresses.
3. A Data tab is referenced for cleaning and preparing strings for email address formats.
4. The Export tab would spew out a list of clean email addresses that can be exported to CSV.
The Data tab is just 2 columns to use with SUBSTITUTE so that for instance apostrophes are removed but accented letters are normalized (é -> e). I've used LAMBDA within Names to get there. The problem is to tie everything in - to get those Named ranges into the final formula.
The Names I'm using so far (I'd like to use fewer but testing specific parts extended beyond simple usage I fear):
ALPH ={"A";"B";"C";"D";"E";"F";"G";"H";"I";"J";"K";"L";"M";"N";"O";"P";"Q";"R";"S";"T";"U";"V";"W";"X";"Y";"Z"}
LABELS =LAMBDA(labelname,ADDRESS(2,MATCH(labelname,INDIRECT("'"&PARAMETERS!$A$2&"'!$1:$1"),0),1,1,PARAMETERS!$A$2))
RANGECOL =LAMBDA(labelname,COLUMN(INDIRECT(LABELS(labelname))))
RNCOL =LAMBDA(label,"'"&PARAMETERS!$A$2&"'!$"&INDEX(ALPH,RANGECOL(label))&":$"&INDEX(ALPH,RANGECOL(label)))
I haven't tied everything in the Data tab yet - I'm still trying to automate my main tab before pushing further and using the Data tab substitutions on top of everything. That will be the next step, not my current focus. But, for the curious and interested, on the Data tab I'm using something something I found on ablebits which works wonders =]
So, now if I use the offset range with a static LIST!A:A it works:
=IF($C$2<>"",LOWER(INDEX(OFFSET(INDIRECT(ADDRESS(2,MATCH($C$2,INDIRECT("'"&$A$2&"'!$1:$1"),0),1,1,$A$2)),0,0,COUNTA(LIST!A:A)-1,1),ROW())),"") &IF($C$3<>"","."&LOWER(INDEX(OFFSET(INDIRECT(ADDRESS(2,MATCH($C$3,INDIRECT("'"&$A$2&"'!$1:$1"),0),1,1,$A$2)),0,0,COUNTA(LIST!A:A)-1,1),ROW())),"") &"#"&$C$4
But when I try to use the dynamic RNCOL($C$3) it does not:
=IF($C$2<>"",LOWER(INDEX(OFFSET(INDIRECT(LABELS($C$2)),0,0,COUNTA(INDIRECT(RNCOL($C$2)))-1,1),ROW())),"") &IF($C$3<>"","."&LOWER(INDEX(OFFSET(INDIRECT(LABELS($C$3)),0,0,COUNTA(INDIRECT(RNCOL($C$3)))-1,1),ROW())),"") &"#"&$C$4
This just gives #REF, and evaluating shows the digression starting at INDIRECT(RNCOL($C$3)) equating to #VALUE.
I'm starting to see double here but my undying and completely normal love for Excel prevents me from going home from work as I'm way too far down the rabbit hole to let my obsession die here.
Any pointers as to how this can work?
Note - all of the names in the supplied sheet were generated by an online fake name generator, nothing in here is actual user data #GDPR
Thanks in advance! <3
Test sheet is available via Google Drive.
Your current set-up is not good for many reasons, and in my opinion would require a complete overhaul, the scope of which lies beyond a response on this website.
As to a 'quick fix' to your current issue, the reason your formula in E1 is currently returning an error is due to the fact that, as you can see via stepping through with the Evaluate Formula tool, the part
COUNTA(INDIRECT(RNCOL($C$2)))-1
is resolving to
COUNTA(INDIRECT({"'LIST'!$A:$A"}))-1
and this is not the same as
COUNTA(INDIRECT("'LIST'!$A:$A"))-1
in that the value being passed to INDIRECT is an array in the former though not in the latter. Although INDIRECT can accept arrays, it is only within certain constructions in conjunction with other suitable functions; here it will simply error.
And the reason that it is returning an array is due to the fact that RNCOL($C$2) is returning an array, and that is because that function is defined as
=LAMBDA(label,"'"&PARAMETERS!$A$2&"'!$"&INDEX(ALPH,RANGECOL(label))&":$"&INDEX(ALPH,RANGECOL(label)))
and, since RANGECOL($C$2) resolves to 1 here, the above is equivalent to
"'PARAMETERS!$A$2'!$"&INDEX(ALPH,1)&":$"&INDEX(ALPH,1)
Here, because you are omitting the column_num parameter from INDEX, the part
INDEX(ALPH,1)
is resolving to
{"A"}
which is an array (albeit one comprising a single value) and technically different from
"A"
In most circumstances, this is not an issue. As such, it is almost always unnecessary to pass both a row_num and column_num parameter to INDEX when indexing a one-dimensional array. Here, however, it matters.
You can resolve this by explicitly including a column_num parameter, i.e. redefine RNCOL as
=LAMBDA(label,"'"&PARAMETERS!$A$2&"'!$"&INDEX(ALPH,RANGECOL(label),1)&":$"&INDEX(ALPH,RANGECOL(label),1))

EXCEL - Circular Reference Error when Entering Data

I've got another one that is really kicking my butt.
I know why the error is occurring. I just don't know how to fix it. There are a lot of IF statements so I'm thinking maybe they are arranged in a way that is causing the error. I'm sure there is a much cleaner way to write them.
Whenever I try to input data into D7 or E7 I get the circular reference error.
These are my current formulas for all relevant cells:
F7: =IF(C7<0,"FAIL",IF(OR(ISBLANK(B7),ISBLANK(C7))," ",IF(ABS(F8)<=30,"PASS","FAIL")))
G7: =IF(D7<0,"FAIL",IF(OR(ISBLANK(B7),ISBLANK(D7))," ",IF(ABS(G8)<=30,"PASS","FAIL")))
H7: =IF(E7<0,"FAIL",IF(OR(ISBLANK(B7),ISBLANK(E7))," ",IF(ABS(H8)<=30,"PASS","FAIL")))
F8: =IF(B7<0,"",IF(C7<0,"",IF(D7<0,"",IF(E7<0,"",IF(G7="FAIL","",IF(H7="FAIL","",IF(ISBLANK(B7),IF(ISBLANK(C7),"","input Lw_Lw"),IF(ISBLANK(C7),"input Lw_Up",SUM(C7-B7)))))))))
G8: =IF(B7<0,"",IF(C7<0,"",IF(D7<0,"",IF(E7<0,"",IF(F7="FAIL","",IF(H7="FAIL","",IF(ISBLANK(B7),IF(ISBLANK(D7),"","input Lw_Lw"),IF(ISBLANK(D7),"input Up_Lw",SUM(D7-B7)))))))))
H8: =IF(B7<0,"",IF(C7<0,"",IF(D7<0,"",IF(E7<0,"",IF(G7="FAIL","",IF(F7="FAIL","",IF(ISBLANK(B7),IF(ISBLANK(E7),"","input Lw_Lw"),IF(ISBLANK(E7),"input Up_Up",SUM(E7-B7)))))))))
Snip of Excel table
Any help would be much appreciated!
I worked out the following two formulas for you. Please try them.
[F7] =IF(IFERROR(ABS(F8)<=30,FALSE),"PASS","FAIL")
[F8] ==IF(ISBLANK(C7),"input "&F$3,IF(OR(COUNT($B7:$E7)<4,COUNTIF($B7:$E7,"<0")>0),"",SUM(C7-$B7)))
Copy them from F7:F8 to G7:H8.
The basic principle I applied in order to avoid a circular reference is to do all testing in row 8 with the outcome that F8 will either hold a blank or a number. Therefore "Pass" or "Fail" in row 7 can be decided based on the number: If the number is within range it's a PASS, else it's failed. Note that Abs("") will cause an error. Therefore IFERROR(ABS(F8)<=30,FALSE) will return False in case F8 = "". Effectively, this is the reverse of what I wrote in my comment above.
I looked for shorter expressions for other tests as well. Count() will only count cells with numbers in them. Therefore I use this function instead of a series of ISBLANK() queries. Similarly for COUNTIF($B7:$E7,"<0").
I used mixed absolute and relative addressing to enable copying formulas to other columns and referred to the column captions in place of repeating the same texts in the formulas.
I didn't fully test my solution. However, with my above explanation as a guide you should be able to take possession and eliminate any errors I might have left behind.

Having hangup with IF AND OR functions. Need one last tweak

Disclaimer: I have never written code in Excel before. I have been building something for work during few days. I have a formula that WORKS! But there is one adjustment that I need to make, that I cannot make happen. Here is what I have. (Again, if its messy, I have no idea, but its currently working).
=IF(AND(D5="Yes LSC, PA's passed")*F5>G5-20,"AT0 Cannot be in same month as W/D",IF(AND(D5="Yes LSC, PA's not passed")*F5>G5-20,"AT0 Cannot be in same month as W/D",IF(AND(E5="Yes, systematic W/D")*F5>G5-20,"AT0 Cannot be in same month as a systematic W/D","")))
Background: I need an excel spreadsheet that we give to new employees as a "cheatsheet" It gives them prompts (I.E "Yes, LSC passed, or Yes, W/D"). They select the prompts and pass to a supervisor for processing.
Goal: I'm trying to write a formula that does not allow simple errors. For my purposes, I know Cell G5 must be AT LEAST 20 greater than F5. So I want an error message to appear every time they try to enter a value that breaks that rule. (Saves management time) And all of that WORKS!
Problem: If G5 is BLANK...It still gives me the message that I've told it to. However, IF G5 is BLANK, I don't need any of the formula to run. I have been trying to build this exception into my amateur formula but I cant get it.
I can understand whats happening.... Excel recognizes that if G5 is blank...then F5>G5-20 is TRUE -- And it gives me text that I want!! But I need an exception... I need it say understand that if G5 is BLANK, then I don't care about F5>G5-20. It can ignore the ENTIRE formula.
Any suggestions? I hope this makes sense.
How would this be?
=IF(ISBLANK(G5),"-", //insert your forumla here// )
an error message to appear every time they try to enter a value that breaks that rule is often handled with Data Validation rather than an IF function. I take it F5 will be completed before G5. Select G5, DATA > Data Tools - Data Validation, Data Validation..., Settings, Allow: Custom, Formula:
=G5>=F5+20
With other settings in their default state this should allow G5 to be blank (or to be populated and then blanked) but give a warning sound and message "The value you entered is not valid." at any attempt to key in an non-compliant value. The warning message may be customised and there is also an option to show an input message where entry of an invalid value is attempted, which might be used to explain the requirement. Alternatively the value might be accepted but later marked as being non-compliant.
It is though rather easy to bypass the validation - just copying from elsewhere and pasting into G5 could do so. Though copying from elsewhere and pasting into the cell with the IF formula would bypass that control too.
Data Validation might also substitute for some of the rest of your IF formula.

Two Nested IF Statements in Excel: Not Detecting All Satisfied Instances

I've got results from a task where the participant had to report whether or not a certain "target" was present or absent. I'd like to compare their response to the actual state of the target (present or absent), and classify their responses as either "Hit" "Miss" "Correct Rejection" or "False Alarm". I'm using two IF statements nested inside another IF statement.
Right now, Excel is only returning the "value_if_false" for each of the nested IF statements.
=IF(G6=F6,IF(F6="Present", "Hit", "Correct Rejection"),IF(F6="Present","Miss","False Alarm"))
Here is a screenshot of what I've got going on.
I've done a lot of searching to find a solution and have tried several different strategies to no avail. Any help is appreciated!
This is not related to nested IF statements. You have either leading or trailing (or both) white spaces in the text in the cells you are comparing.
Instead of cell F6 containing the text "Absent", it is actually " Absent" or "Absent ", or a mixture of the two.
To fix do one of:
Use the TRIM function in your IF statement.
Remove the extra spaces from your cells.
Add the white spaces to your IF statement.
Fix 1 is easiest, probably. So your formula should be changed to
=IF(TRIM(G6)=TRIM(F6),IF(TRIM(F6)="Present","Hit","Correct Rejection"),IF(TRIM(F6)="Present","Miss","False Alarm"))

If Then Loop with Search in Excel

I am currently doing a "if then" loop in excel with following logic.
Value to Check
1. S9(7) C3
2. S9(11)V9(2) C3
3. X(3)
Logic I have used
=IF(ISNUMBER(SEARCH("S9",D1,1)),CONCATENATE("s370fpd",5,".","0"),CONCATENATE("$ebcdic",5,"."))
Now problem here is that S9 maybe of two types. In the first one i.e. S9(7) C3
it works fine but for second one i.e. S9(11)V9(2) C3 I have to CONCATENATE "s370fpd" with value inside bracket of V9 i.e. 2 in this case. So it will become "s370fpd5.2".
Since the value inside bracket is dynamic hence the logic has to search inside it. I am not sure how it will check and then append it.
I'm not sure what you're trying to do exactly, but keeping as much as what you have right now as possible, I would use this:
=IF(ISNUMBER(SEARCH("S9",D1,1)),CONCATENATE("s370fpd",5,".",IFERROR(MID(D1,SEARCH("V9",D1)+3,SEARCH(")",D1,SEARCH("V9",D1))-SEARCH("V9",D1)-3),"0")),CONCATENATE("$ebcdic",5,"."))
I substituted "0" with
IFERROR(MID(D1,SEARCH("V9",D1)+3,SEARCH(")",D1,SEARCH("V9",D1))-SEARCH("V9",D1)-3),"0")
which is what gets what's between brackets after V9.
It also appears to me that you can turn CONCATENATE("$ebcdic",5,".") into "$ebcdic5." directly and CONCATENATE("s370fpd",5,"." into CONCATENATE("s370fpd5.", and/or use & to make things a little shorter, but that's only subject to what you've put in your question:
=IF(ISNUMBER(SEARCH("S9",D1,1)),"s370fpd5."&IFERROR(MID(D1,SEARCH("V9",D1)+3,SEARCH(")",D1,SEARCH("V9",D1))-SEARCH("V9",D1)-3),"0"),"$ebcdic5.")

Resources