formula too long

d0wnt0wn

Well-known Member
Joined
Oct 28, 2002
Messages
771
hey guys... any way I can shorten this formula so I can use it. says I have too many conditions

=IF(E16>16,D16*1.8,IF(E16>14,D16*1.67,IF(E16>12,D16*1.54,IF(E16>11,D16*1.41,IF(E16>9,D16*1.3,IF(E16>7,D16*1.2,IF(E16>5,D16*1.12,IF(E16>4,D16*1.08,if(D16>3,D16*1.06,if(d16>2,d16*1.04,if(d16>1,d16*1.03,if(d16>0,d16*1.02,if(d16.-1,d16*1,)))))))))))))
 
Thanks Doc
clip_image001.gif

You’re welcome:

.1 Make sure you check out Snakehips stuff. Its very informative

.2 Wot was your prob in the end?

.3 Be a bit careful with the typos!
…. And the Thread Tiltle… Your formula is not too long!, (at least for fromXL2007 upwards)….
…I am just going through threads now where they use up to the full allowed 64!!
….In desperation I knocked up just now this code to write out a table of the conditions…

Code:
[color=darkblue]Option[/color] [color=darkblue]Explicit[/color]
[color=darkblue]Sub[/color] SplitIF() [color=green]'Simple Split of long Nested IF[/color]
    [color=darkblue]Dim[/color] IF_Formula [color=darkblue]As[/color] [color=darkblue]String[/color], IF_Line() [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'Formula in String Form,Split at IF lines[/color]
    [color=darkblue]Dim[/color] IF_Line_Index [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]'A number for use in Array Index or Cell Row[/color]
    [color=green]'Put your formula in a cell with enough free cells under it, add someting before the = to change it to text[/color]
    [color=darkblue]Let[/color] IF_Formula = ActiveCell.Value [color=green]'Get Formula from the cell you just put it in[/color]
    IF_Formula = Replace(IF_Formula, "If", "IF") [color=green]'Put all IFs in same syntax[/color]
    IF_[color=darkblue]For[/color]mula = Replace(IF_Formula, "if", "IF")
        IF_Line = Split(IF_Formula, "IF") [color=green]'Split that formula using IFs as seperator[/color]
       
        For IF_Line_Index = 1 [color=darkblue]To[/color] [color=darkblue]UBound[/color](IF_Line) [color=green]'Arrays star at 0 Index but we skip the first bit with =[/color]
            ActiveSheet.Cells(ActiveCell.Row + IF_Line_Index, 1).Value = IF_Line(IF_Line_Index) [color=green]'Write underformila the split line[/color]
        [color=darkblue]Next[/color] IF_Line_Index
       
[color=darkblue]End[/color] [color=darkblue]Sub[/color] [color=green]'SplitIF()[/color]

.. For fun if you apply it to your and my versions of your original formula you get this, which is a bit more readable


Book1
A
20d0wnt0wnALan=IF(E16>16,D16,IF(E16>14,(D16*1.67),IF(E16>12,D16*1.54,IF(E16>11,D16*1.41,IF(E16>9,D16*1.3,IF(E16>7,D16*1.2,IF(E16>5,D16*1.12,IF(E16>4,D16*1.08,IF(D16>3,D16*1.06,IF(D16>2,D16*1.04,IF(D16>1,D16*1.03,IF(D16>0,D16*1.02,IF(D16>1.01,D16*1,)))))))))))))
21(E16>16,D16,
22(E16>14,(D16*1.67),
23(E16>12,D16*1.54,
24(E16>11,D16*1.41,
25(E16>9,D16*1.3,
26(E16>7,D16*1.2,
27(E16>5,D16*1.12,
28(E16>4,D16*1.08,
29(D16>3,D16*1.06,
30(D16>2,D16*1.04,
31(D16>1,D16*1.03,
32(D16>0,D16*1.02,
33(D16>1.01,D16*1,)))))))))))))
34
35d0wnt0wn=IF(E16>16,D16*1.8,IF(E16>14,D16*1.67,IF(E16>12,D16*1.54,IF(E16>11,D16*1.41,IF(E16>9,D16*1.3,IF(E16>7,D16*1.2,IF(E16>5,D16*1.12,IF(E16>4,D16*1.08,if(D16>3,D16*1.06,if(d16>2,d16*1.04,if(d16>1,d16*1.03,if(d16>0,d16*1.02,if(d16.-1,d16*1,)))))))))))))
36(E16>16,D16*1.8,
37(E16>14,D16*1.67,
38(E16>12,D16*1.54,
39(E16>11,D16*1.41,
40(E16>9,D16*1.3,
41(E16>7,D16*1.2,
42(E16>5,D16*1.12,
43(E16>4,D16*1.08,
44(D16>3,D16*1.06,
45(d16>2,d16*1.04,
46(d16>1,d16*1.03,
47(d16>0,d16*1.02,
48(d16.-1,d16*1,)))))))))))))
Tabelle1


Alan
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
…I am just going through threads now where they use up to the full allowed 64!!
….In desperation I knocked up just now this code to write out a table of the conditions…
.. For fun if you apply it to your and my versions of your original formula you get this, which is a bit more readable

Alan,

Are you sad or wot??? ;)

It's comforting to know that I am not alone!!

A nice little idea to break out the nested if's.

A couple of comments......

'Put all the IF's in the same syntax' If you copy the confirmed formula from it's cell to the test cell then will not any IF's have been automatically capitalised? Thus unless you add your prefix and edit any IF's to e.g. If then there is no ned to check?
Also, if you have some text in the formula that includes e.g. "if" it will convert that to "IF" and it becomes an erroneous separator in the Split function.
Another thought is that splitting on IF will incorrectly split at the backend of COUNTIF, SUMIFS etc etc.
Finally, your code lists the breakdown only in column A.


Being a sad b*s!*rd, with nothing better to do than mess with Excel for no good reason other than because 'it's there', I have modified your original code, below.

You can edit one code line, ('********), by having it as code line or comment, to have the formula in the test cell retained as formula, in editable form or converted to text by adding the prefix.
As before you need empty cells below the test cell but the test cell can be in any column.
I may not have covered all bases but the Replace functions are looking for the likes of "=IF(" ",IF(" as valid separation points. Such points then replaced with the .normally, unlikely text "§§" to be used as the Split delimiter.


Code:
Sub SplitIF2() 'Split of long Nested IF


    Dim IF_Formula As String, IF_Line() As String 'Formula in String Form,Split at IF lines
    Dim IF_Line_Index As Long 'A number for use in Array Index or Cell Row
    
    '**** Put your formula in a cell with enough free cells under it,
    
    IF_Formula = "Split IFs >>> " & ActiveCell.Formula 'adds something before the = to change it to text


    'comment out the below line if you wish to retain and be able to edit the formula  
    '*********
    ActiveCell.Value = IF_Formula   '*** converts cell formula to text if you wish
    '*********  

    
    IF_Formula = Replace(Replace(Replace(IF_Formula, ",IF(", ",§§("), "=IF(", "=§§("), "(IF(", "(§§(") 'Put all IFs in same syntax
        IF_Line = Split(IF_Formula, "§§") 'Split that formula using §§ as seperator
       
        For IF_Line_Index = 1 To UBound(IF_Line)
            ActiveSheet.Cells(ActiveCell.Row + IF_Line_Index, ActiveCell.Column).Value = IF_Line(IF_Line_Index)   'Write under formula the split line
        Next IF_Line_Index
       
End Sub 'SplitIF()

The two approaches compare as below with the sample formula....

Excel Workbook
AB
1Doc_Split =IF(B6>B8,"More",IF(B8Split IFs >>> =IF(B6>B8,"More",IF(B8
2(B6>B8,"More",(B6>B8,"More",
3(B8(B8
4ference",COUNT
5(A6:A9,6)))
Sheet1
 
Upvote 0
Alan,

More sad thoughts...

Why not split into separate lines within the formula bar?

With the actual formula cell selected, run.....

Code:
Sub SplitIF_InCell() 'Split of long Nested IF within it's cell


    Dim IF_Formula As String  'Formula in String Form
        
    'Run code with formula cell selected
    
        IF_Formula = ActiveCell.Formula  'adds something before the = to change it to text
        
        IF_Formula = Replace(Replace(Replace(IF_Formula, ",IF(", ",§§("), "=IF(", "=§§("), "(IF(", "(§§(")
        IF_Formula = Replace(IF_Formula, "§§", Chr$(10) & "IF")
        ActiveCell.Formula = IF_Formula
     Application.FormulaBarHeight = 10
End Sub

Then to convert back to a one line formula, with or without any formula editing, run......

Code:
Sub UnSpitIf_InCell()
'revert to single line formula...
ActiveCell.Formula = Trim(WorksheetFunction.Clean(ActiveCell.Formula))
Application.FormulaBarHeight = 1
End Sub
 
Last edited:
Upvote 0
wow look at this thread go lol..... thanks so much for the help guys I have learned a lot just from reading this thread... and yes the first formula was a typo :)
 
Upvote 0
Tony,
Alan,

Are you sad or wot???

It's comforting to know that I am not alone!!

…….
I try to be sad with VBA as often as I can which is not too often just now so it is great when other more experienced sad b*s!*rds take the time to help. Thanks very much I appreciate it.

…If you copy the confirmed formula from it's cell to the test cell then will not any IF's have been automatically capitalized? Thus unless you add your prefix and edit any IF's to e.g. If then there is no need to check?...
. I expected it might be… and yep.. just checked on my German formulas..You are right. The extra problem is that I work in German so =IF … etc in a cell just errors and by hitting escape I am left with the formula in its original text with IFs and ifs etc.

….Also, if you have some text in the formula that includes e.g. "if" it will convert that to "IF" and it becomes an erroneous separator in the Split function.
Another thought is that splitting on IF will incorrectly split at the backend of COUNTIF, SUMIFS etc etc.
Finally, your code lists the breakdown only in column A.
Being a sad b*s!*rd, with nothing better to do than mess with Excel for no good reason other than because 'it's there', I have modified your original code, below.

.1 You can edit one code line, ('********), by having it as code line or comment, to have the formula in the test cell retained as formula, in editable form or converted to text by adding the prefix.
.2 Finally, your code lists the breakdown only in column A.
.3 I may not have covered all bases but the Replace functions are looking for the likes of "=IF(" ",IF(" as valid separation points. Such points then replaced with the .normally, unlikely text "§§" to be used as the Split delimiter.
……….

.1 I wanted to do that but did not know how. So many thanks. I noticed something strange , interesting and very useful to me…..
.1 a
. This
Code:
IF_Formula = "Split IFs >>> " & ActiveCell.Formula 'adds something before the = to change it to text
    ActiveCell.Value = IF_Formula   ' converts cell formula to text if you wish

….Does exactly wot you said AND converts a German Formula to English which I have been trying to find a way of doing (instead of cluttering up the Test Forum using the HTML maker which seems to do that automatically)***
…….
.1 b
If I do this instead
Code:
IF_Formula = ActiveCell.Formula 
    ActiveCell.Value = IF_Formula
… I just get the value that the formula returns, not the formula as text - strange. (Maybe a German to English anomaly)
.2 The ActiveCell.Column instead of 1 was a typo or rather dumb error on my behalf that I (Honest!) noticed just before I went through your code – I had wanted it all to be under the Formula.). But please continue to point out my mistakes. That is very helpful as I make lots!
.3 I am at least as sad as you and also wanted to do that bit but I have an empty space in my brain where you have VBA knowledge, so I was unsure or rather have never encountered things like COUNTIF, SUMIFS etc etc. But I should have thought about text with if in it – I just did a code to help in the Threads I was looking at which did not have any. But I prefer your better code.
Thanks for the improvement; I appreciate the benefit of your experience there.

..
More sad thoughts...

Why not split into separate lines within the formula bar?
With the actual formula cell selected, run.....
.........
Then to convert back to a one line formula, with or without any formula editing, run......
........

. Please stay sad. :) Again this is the sort of thing I would like ideally to have, but lack the experience to produce it in a reasonable time. So in the meantime I am very grateful for all your help and suggestions.
Thanks again
Alan.

(p,s, ** If you stumble across something that converts English formulas to German please let me know – At the moment I have to rely on people in the thread sending me a file with formulas in it which usually converts automatically to German when I open the file in my German Excel
 
Upvote 0
Hi,
. I wanted to try and put a bit back in this thread. I thought I would try something simple within my abilities and complete the set of codes to do one to produce the IF Formula from a set of conditions… I actially got down on the first bit as strangely I could not seem to get the usual “End(XLUp) or Find” etc to get number of conditions. But I did it with a Bodge that I think works.
. So here is the code:

Code:
[color=darkblue]Sub[/color] UnSplitIF()
  [color=green]'Select First condition Cell and make sure cells around the perimiter of the ondition Columns are empty or the Currentrange part captures too much and[/color]
  
  [color=green]'*************[/color]
  'This bit confused me and was just trial and error to get the number of conditions
  [color=darkblue]Dim[/color] ConditionsDataThing [color=darkblue]As[/color] [color=darkblue]Variant[/color] [color=green]'I thpought this was an Array as it acts for example with the  UBound as one. But it would not let me dimension it as one??[/color]
  [color=darkblue]Let[/color] ConditionsDataThing = Cells(ActiveCell.Row, ActiveCell.Column).CurrentRegion [color=green]'Capture the "area" of the conditions.[/color]
  [color=darkblue]Dim[/color] ConditionLineNumber [color=darkblue]As[/color] [color=darkblue]Long[/color], MaxConditionIndex [color=darkblue]As[/color] [color=darkblue]Long[/color]
  [color=darkblue]Let[/color] MaxConditionIndex = [color=darkblue]UBound[/color](ConditionsDataThing)
  [color=green]'***************[/color]
  [color=darkblue]ReDim[/color] ConditionsArray(ActiveCell.Row [color=darkblue]To[/color] ActiveCell.Row + MaxConditionIndex - 1) [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'This is a normal Array. I give it here Indicies equal to the cell indicies of the Conditions Column. I must use ReDim as [color=darkblue]Dim[/color] only takes numbers not variables[/color]
  [color=darkblue]For[/color] ConditionLineNumber = ActiveCell.Row [color=darkblue]To[/color] ActiveCell.Row + MaxConditionIndex - 1 [color=green]'Go through each condition and..[/color]
    [color=darkblue]Let[/color] ConditionsArray(ConditionLineNumber) = Cells(ConditionLineNumber, ActiveCell.Column).Value [color=green]'..put each condition from the cells into the Conditions Array[/color]
  [color=darkblue]Next[/color] ConditionLineNumber
  
  [color=darkblue]Let[/color] ActiveCell.Offset(-1, 0).Value = "UnSplitIFFormula  >>> =IF" & Join(ConditionsArray, "IF") [color=green]'Write out the formula in the cell just above the Conditions column[/color]
  
  Dim IF_Formula [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'Formula again without extra text before the =[/color]
  [color=darkblue]Let[/color] IF_Formula = "=IF" & Join(ConditionsArray, "IF")
  [color=green]'ActiveCell.Offset(-1, 1).Value = IF_Formula'Could not check this as it erors probably as the formula is English an my Excel is german[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

. If you run it on something like this:….


Book1
EFG
24
25(E16>16,D16,
26(E16>14,(D16*1.67),
27(E16>12,D16*1.54,
28(E16>11,D16*1.41,
29(E16>9,D16*1.3,
30(E16>7,D16*1.2,
31(E16>5,D16*1.12,
32(E16>4,D16*1.08,
33(D16>3,D16*1.06,
34(D16>2,D16*1.04,
35(D16>1,D16*1.03,
36(D16>0,D16*1.02,
37(D16>1.01,D16*1,)))))))))))))
38
Tabelle1


…..you get something like this.


Book1
EFG
23
24UnSplitIFFormula >>> =IF(E16>16,D16,IF(E16>14,(D16*1.67),IF(E16>12,D16*1.54,IF(E16>11,D16*1.41,IF(E16>9,D16*1.3,IF(E16>7,D16*1.2,IF(E16>5,D16*1.12,IF(E16>4,D16*1.08,IF(D16>3,D16*1.06,IF(D16>2,D16*1.04,IF(D16>1,D16*1.03,IF(D16>0,D16*1.02,IF(D16>1.01,D16*1,)))))))))))))
25(E16>16,D16,
26(E16>14,(D16*1.67),
27(E16>12,D16*1.54,
Tabelle1


. Note because of the bodge I used, the cells around the perimeter of the Conditions column must be empty!
(I could not check the last bit to give a real formula as my German Excel does not recognize it and errors if I try to put it in a “German” cell!)

Alan

P.s. Here is my current (XL 2007 .xlsm) File that has all macros to date in from us
https://app.box.com/s/fd5vyegpkn83bn3z922t
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top