Unable to add if statement as exceeds level of nesting allowed? - excel

I have a list of 5 if statements in my formula, i am wanting to add a 6th, but it won't let me and says I have exceeded the level of nesting allowed.
Can someone please show me how I can add the following to my code?
IF(COUNT(SEARCH({"BTF","BTH","BTO","BTP"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_2.msg"),"")
Code:
=IF(COUNT(SEARCH({"XWS","XWW","XWO","XVV","XVS","XVO","XVH","XTO","XTA","XSW","XSV","XST","XSS","XSS","XSR","XSP","XSL","XSJ","XSH","XSG","XSF","XSE","XSD","XSC","XSB","XSA","XS5","XS2","XPO","XMO","XMF","XLS","XLP","XLO","XLL","XLB","XKT","XKR","XKO","XKH","XKE","XKD","XJS","XHO","XHL","XHF","XHA","XGO","XFT","XFO","XFC","XFA","XDO","XBS","XBO","XAO","WHO","PSO","PRO","PRM","PRE","PPT","PLO","PGW","PGV","PGT","PGS","PGR","PGP","PGH","PGF","PGE","PGB","PGA","PFP","PDC","PDB","PCP","PBO","OFD","MWW","MWC","MTT","MSP","MSO","MRM","MPP","MPO","MPF","MNO","MMW","MMS","MMP","MMI","MLO","MJO","MHH","MGO","MFO","MEE","MEB","MDO","MCO","MAT"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_PLANT2.msg"),
IF(COUNT(SEARCH({"XCT","XCO","XCA"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_CRANE2.msg"),
IF(COUNT(SEARCH({"XJO","XJS"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_OPERATOR2.msg"),
IF(COUNT(SEARCH({"LTV","LTH","LSW","LMC"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_3PL2.msg"),
IF(COUNT(SEARCH({"UUU","KKK","PPP","ACO","ARC","BCO","BCP","BFA","BFI","BFL","BFP","BHO","BPO","BSO","BTF","BTH","BTO","BTP","CMA","CME","CMF","CMH","CML","CMN","CMP","CMZ","DHA","EAA","EAP","ECC","ECC","ECE","EPM","EPO","FPA","FPB","FPC","FPD","FPE","FPF","FPH","FPI","FPJ","FPL","FPM","FPN","FPO","FPP","FPR","FPS","FPV","FPW","FSC","FUE","FUG","FUW","FWC","FWG","FWH","HPO","ICH","ICM","ICN","ICS","ITL","ITM","QMO","RSS","RTT","SFG","SLB","SLC","SLR","SLT","SMA","SMC","SMM","SMP","SMR","SSF","SSG","SSV","STA","STO","UEO","UGO","UWO","ZFO"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_2.msg"), IF(COUNT(SEARCH({"BRL","BRP","BRS"},G18)),HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\EMAIL_RECRUITMENT2.msg"),
""))))))

I agree with #Rowland Shaw regarding a separate table with lookups.
But if you are set on numerous IF() than just return a blank as the false result of each, and concatenate all the results.
=
HYPERLINK("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\"
&
IF(COUNT(SEARCH({"XWS","XWW","XWO","XVV","XVS","XVO","XVH","XTO","XTA","XSW","XSV","XST","XSS","XSS","XSR","XSP","XSL","XSJ","XSH","XSG","XSF","XSE","XSD","XSC","XSB","XSA","XS5","XS2","XPO","XMO","XMF","XLS","XLP","XLO","XLL","XLB","XKT","XKR","XKO","XKH","XKE","XKD","XJS","XHO","XHL","XHF","XHA","XGO","XFT","XFO","XFC","XFA","XDO","XBS","XBO","XAO","WHO","PSO","PRO","PRM","PRE","PPT","PLO","PGW","PGV","PGT","PGS","PGR","PGP","PGH","PGF","PGE","PGB","PGA","PFP","PDC","PDB","PCP","PBO","OFD","MWW","MWC","MTT","MSP","MSO","MRM","MPP","MPO","MPF","MNO","MMW","MMS","MMP","MMI","MLO","MJO","MHH","MGO","MFO","MEE","MEB","MDO","MCO","MAT"},G18)),"EMAIL_PLANT2.msg","")
&
IF(COUNT(SEARCH({"XCT","XCO","XCA"},G18)),"EMAIL_CRANE2.msg","")
&
IF(COUNT(SEARCH({"XJO","XJS"},G18)),"EMAIL_OPERATOR2.msg","")
&
IF(COUNT(SEARCH({"LTV","LTH","LSW","LMC"},G18)),"EMAIL_3PL2.msg","")
&
IF(COUNT(SEARCH({"UUU","KKK","PPP","ACO","ARC","BCO","BCP","BFA","BFI","BFL","BFP","BHO","BPO","BSO","BTF","BTH","BTO","BTP","CMA","CME","CMF","CMH","CML","CMN","CMP","CMZ","DHA","EAA","EAP","ECC","ECC","ECE","EPM","EPO","FPA","FPB","FPC","FPD","FPE","FPF","FPH","FPI","FPJ","FPL","FPM","FPN","FPO","FPP","FPR","FPS","FPV","FPW","FSC","FUE","FUG","FUW","FWC","FWG","FWH","HPO","ICH","ICM","ICN","ICS","ITL","ITM","QMO","RSS","RTT","SFG","SLB","SLC","SLR","SLT","SMA","SMC","SMM","SMP","SMR","SSF","SSG","SSV","STA","STO","UEO","UGO","UWO","ZFO"},G18)),"EMAIL_2.msg","")
&
IF(COUNT(SEARCH({"BRL","BRP","BRS"},G18)),"EMAIL_RECRUITMENT2.msg","")
)

Related

VBA Dynamically Building A Formula From An Array

I am trying to dynamically construct a formula based on an array that I have generated from a cell (separated by commas), as there is a varying amount of elements in the array I need to append a new "formula block" with the updated element to use in a if statement that is generated after the for each loop. VBA is throwing a type mismatch error in the InvestigateFormula = line, here is my code:
For Each Type In ToIgnore()
InvestigateFormula = "(ISNUMBER(SEARCH(*" & ToIgnore(Type) & "*," & _
AssetTypesCol & "2)),"
FullFormula = InvestigateFormula & FullFormula
Next Asset
FinalInvestigateFormula = "=IF(OR" & FullFormula & "),""Ignore"", """")"
ActiveCell.Formula = FinalInvestigateFormula
Please let me know if there is an easier way of doing this or how I might be able to correct the above code. Btw I am not declaring a variant I am simply declaring ToIgnore() as String and using the split function from the variable which contains the comma separated values to generate the array/items to loop over.
"Type" is a reserved name? Try strType instead?

How do you look for a condition in a range and then get info from another range into a single cell?

So at the moment I have a working code to get multiple items in a cell following a condition
My code is:
=Contatenate(
IF('Sheet1'!E3 = "R",'sheet1'!A3,""), " ",
IF('Sheet1'!E4 = "R",'sheet1'!A4,""), " ",
and so on.....
it then returns every name that meets this condition.
I was just wondering if there was a quicker way to do this.
Thanks!
Leaving the solution as an answer to make it more clear:
=ArrayFormula(TEXTJOIN(" ",TRUE,IF('Contact & Overview'!E103:E122="R",'Contact & Overview'!A103:A122,"")))
From Sheets TEXTJOIN:
Sample Usage
TEXTJOIN(“ “, TRUE, “hello”, “world”)
TEXTJOIN(“, ”, FALSE, A1:A5)
Syntax
TEXTJOIN(delimiter, ignore_empty, text1, [text2, ...])

Rules for escaping formulas and variables

I know it's been asked several times, but I just don't get it.
I'd like to know the rules in order to correctly build VBA code which incorporates formulas and variables in a VBA Evaluate function.
This works ok:
stringa_per_mesi = "=DATEDIF(""" & d1 & """,""" & WorksheetFunction.EDate(d1, 12) & """,""ym"")"
mesi = Application.Evaluate(stringa_per_mesi)
I have to write a more complex formula with Evaluate but I get lost, so I have to solve the doubts I have.
In the above example, why couldn't I just write
stringa_per_mesi = "=DATEDIF(d1, & WorksheetFunction.EDate(d1, 12) & """,""ym"")"
and why this works
WorksheetFunction.EDate(d1, 12)
even without quotation marks here (d1, 12)?
Thank you!

Comparing Textbox value to cell value

I programmed a communication tool for the production floor. This tool will register what they have done, who has done it and on what time.
The following should check whether the textbox value equals the value in the worksheet or if the textbox (textbox is TextTools1) is empty. If this is true, then nothing should happen and the thus the value of the textbox is gonna stay the same.
If the textbox is not empty or is not equal to what has been previously saved in the worksheet (thus the value has changed), then it should be registered which operator has done it and what date and what time.
It works when the textbox is empty, but when the value of the textbox has stayed the same (thus TextTools.value=ActiveCell.Offset(0,23).value (Correct)) it still adds the operators name, date and time.
Something is going wrong when trying to compare the textbox value and the cell value, but cant put my finger on it.
Sheets("Checklist & overdracht").Visible = True
Sheets("Checklist & overdracht").Select
If TextTools1.Value = Range("AZ1").Value Or TextTools1.Value = Empty Then
Sheets("Checklist & overdracht").Select
rowloc1.Value = ActiveCell.Row
ActiveCell.Offset(0, 23).Value = TextTools1.Value
Else
Sheets("Checklist & overdracht").Select
rowloc1.Value = ActiveCell.Row
ActiveCell.Offset(0, 23).Value = TextTools1.Value & " " & "(" & cboOperator.Value & " " & Format(DateValue(CStr(Now)), "short date") & " " & Format(TimeValue(CStr(Now)), "hh:mm") & ")"
End If
Edit; changed it to the code above. I tested this in another userform (and used f8) and it works brilliantly, but when I put in the userform that will actually run this code, than it doesnt have the same result...
Edit2; So apparently something goes wrong with Range(AZ1).Value reference. Because when I enter a random value instead of the range and then run the code, it does work. Is there a different way of referencing?
Ok based on your comments
Stop using active cell when code from a user form is communicating to the compiler what sheet is what. You need to fully qualify what sheet you are using. Im not entirely sure where in the code the active sheet is being set but I am fairly certain the answer is never. Another reason selecting and referencing .ActiveWhatever is bad is a cardinal sin of vba is interacting with the actual application object instead of doing everything in memory. It bogs everything done and performance suffers considerably. When you start writing pretty dense stuff then you will inevitably suffer from issues where the compiler gets confused as to what thing it should be looking at and you'll have a grand ol' time of troubleshooting that nonsense.
Also, it might be a good idea to check for more than just "=Empty". What if there is a null or empty string? I tend to check for:
.value = "" OR ISNULL(.Value)=True OR .Value = vbNullstring
this isnt real feedback though - tons of people have different ways of doing the same thing.
Try:
Thisworkbook.Sheets("YOURSHEETNAME").Range("YOURRANGE").Offset(0,23).Value = Someothervalue.
Let me know if youre still facing issues.

Convert long formula into an ARRAYFORMULA

Document: https://docs.google.com/spreadsheets/d/1N4cGw5eUq_3gCJh1w39qVatX9KV1_Hr-AqRHj_nbckA/edit#gid=1770960621
Question
How can I convert the following simple formulas at Schedule!C20:I29 into a single, simple ARRAYFORMULA at Schedule!C20?
=Count(Filter(Students!$B$5:$B, Find(C6, Filter(Students!$J$5:$O,Students!$J$4:$O$4 = 'Current Class'!$B$3))))
.
NOTE:
The above code is only a partial solution. I will substitute the ARRAYFORMULA version of the code into the correct part of the code at Current Class!L6
The C6 reference above can take on any cell between Schedule!C6:I15. I have named that range Timetable_Code. I thought I could do the following, but I was wrong...
=Arrayformula(Count(Filter(Students!$B$5:$B, Find(Timetable_Code, Filter(Students!$J$5:$O,Students!$J$4:$O$4 = 'Current Class'!$B$3)))))
Background
Originally, I created a table that now resides at 1st Version - Current Class!L6. This tab is only for your reference and will be deleted soon. Each cell has a formula with a slight modification. This formula works correctly; however, it is a behemoth and would be hard to modify...
=if(COUNTIF(Meta!$B$5:$B, CONCATENATE("=",if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1)))), CONCATENATE(if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1)), " ( ", Count(Filter(Students!$B$5:$B, Find(CONCATENATE(if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1))), Filter(Students!$J$5:$O,Students!$J$4:$O$4 = $B$3)))), " )") ,"")
.
Pros
I don't have to create any helper data.
All calculations are "in-memory"
Cons
Too large
Hard to modify
I like the output, but I don't like the cons, so I started to create a more edit-friendly version of the code that I am mostly OK with. This code is located at Current Class!L6 (and a secondary copy at Schedule!C33 - it will be deleted.) It has a single formula at Current Class!L6...
=arrayformula(if(COUNTIF(Meta!$B$5:$B, ("=" & Timetable_Code)), (Timetable_Code & " ( " & Timetable_StudentCount & " )") ,""))
.
Pros
Very easy to understand
Very easy to modify
No need to copy formula over to other cells
Cons
Two ( 2 ) helper tables were created ( one of which I think is unneeded)
Again, I like the output, but I really don't like the second helper table (Schedule!C20). I feel like this table can be eliminated, but I have not been able to figure out how.
If you really want to use arrayformula, here it is. For Schedule!C20.
=arrayformula((len(concatenate(index(Students!J5:O, , match('Current Class'!$B$3, Students!J4:O4, 0))))-len(substitute(concatenate(index(Students!J5:O, , match('Current Class'!$B$3, Students!J4:O4, 0))),C6:I15,"")))/len(C6:I15))
Probably you can use filter(as you did before) instead of index & match part, but I prefer index & match and don't want to dig more. Also you can use one help cell to store filter or index & match result to shorten the formula.
The core idea is from counting occurrences of given character in a string, ie len(a1) - len(substitute(a1, .... You can find many documents about it in the net.
Anyway, if I were you, I'd be satified with the current state. Just lock and hide the help tables or sheets. Nobody cares hidden sheets and if something bad happens, you can revert any change.
After getting a good answer from #Sangbok Lee, I decided to break apart each part of the function he gave to me. While doing that I found a highly unlikely connection to some work I did in the Google Sheets last week. A helper column I had in another tab kind of did what Sangbok Lee was trying to do. All I had to do was split that helper column into two columns (1 for the previous final calculation, 1 for) and calculate an additional count column
After reworking both of our formulas, and testing the result, I found a solution that I am even more satisfied with!
=arrayformula(if(countif(Meta!$B$5:$B, (Timetable_Code)), (Timetable_Code & " ( " & vlookup(Timetable_Code, StudentCount_Lookup, 2, false) & " )") ,""))
.
Check out the differences in the Google Sheet
Look at 1st Version - Current Class!L6 tab for the 1st version
Look at Current Class!L6 for the 2nd version
Look at Current Class!U6 for the 3rd and final version
Also look at tab Meta and Schedule for the differences.
Note: Green is old data, Red is new data

Resources