excel only allows the nesting upto 7 deep any one any ideas on how to increase the nesting say upto 20 x ?? all i have been able to find up to now is that it is possible in vba but no examples
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
nested ‘ IF ‘ more than 7x (officexp 2002)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » nested ‘ IF ‘ more than 7x (officexp 2002)
- This topic has 28 replies, 6 voices, and was last updated 21 years, 6 months ago.
AuthorTopicWSalexanderd
AskWoody LoungerJuly 30, 2003 at 8:34 pm #391274Viewing 4 reply threadsAuthorReplies-
WSsdckapr
AskWoody LoungerJuly 30, 2003 at 8:47 pm #698313There are some tricks to getting around the nested 7 ifs:
Use a user-defined function in VB
Use boolean logic
Use some sort of lookup table with the options
Use a concatenation of possible ifsWhat is best with an example, would be easier if you stated what you want the formula to do. possibly with example data and then you will most likely get multiple ways to accomplish it.
Steve
-
WSalexanderd
AskWoody LoungerJuly 30, 2003 at 9:00 pm #698320the following code is used repatatively to place “yes” in a column in preperation for deleting the line . i would like to be able to increase the function so that i can make the macro run quicker.
—————————————————————————————————————————————————-wsh.Range(“P4”).FormulaR1C1 = _
“=IF(RC[-15]=33070,””yes””,IF(RC[-15]=33080,””yes””,IF(RC[-15]=33180,””yes””,IF(RC[-15]=33126,””yes””,IF(RC[-15]=33085,””yes””,IF(RC[-15]=33185,””yes””,IF(RC[-15]=33087,””yes””,””””)))))))”
wsh.Range(“P4”).Copy Destination:=wsh.Range(“P5:P6000”)
wsh.Range(“Q4”).FormulaR1C1 = _
“=IF(RC[-16]=33090,””yes””,IF(RC[-16]=33190,””yes””,IF(RC[-16]=33091,””yes””,IF(RC[-16]=33093,””yes””,IF(RC[-16]=33095,””yes””,IF(RC[-16]=33094,””yes””,IF(RC[-16]=33101,””yes””,””””)))))))”
wsh.Range(“Q4”).Copy Destination:=wsh.Range(“Q5:Q6000”)
wsh.Range(“R4”).FormulaR1C1 = _
“=IF(RC[-17]=33099,””yes””,IF(RC[-17]=33097,””yes””,IF(RC[-17]=33150,””yes””,IF(RC[-17]=33135,””yes””,IF(RC[-17]=33136,””yes””,IF(RC[-17]=33100,””yes””,IF(RC[-17]=33105,””yes””,””””)))))))”
wsh.Range(“R4”).Copy Destination:=wsh.Range(“R5:R6000”)
Application.CutCopyMode = False
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
Calculate
Range(“A4”).Select
Selection.AutoFilter
Range(“A4:R6000”).Select
lastrow = ActiveSheet.UsedRange.Rows.Count
For r = lastrow To 1 Step -1
If LCase(Cells(r, 15).Value) = “yes” Then Rows®.Delete
If LCase(Cells(r, 16).Value) = “yes” Then Rows®.Delete
If LCase(Cells(r, 17).Value) = “yes” Then Rows®.Delete
If LCase(Cells(r, 18).Value) = “yes” Then Rows®.Delete
Next r -
WSsdckapr
AskWoody LoungerJuly 30, 2003 at 10:36 pm #698361This doesn’t really answer the question of your original post, but I think it should SOLVE the underlying problem.
Another option (if you have the list of numbers in a named range in the spreadsheet is just to loop thru the range instead of creating the array.
Steve
Option Explicit Public BadArray As Variant Sub DeleteBad() Dim wsh As Worksheet Dim lastrow As Long Dim r As Long Application.ScreenUpdating = False BadArray = Array(33070, 33080, 33180, 33126, _ 33085, 33185, 33087, 33090, 33190, _ 33091, 33093, 33095, 33094, 33101, _ 33099, 33097, 33150, 33135, 33136, _ 33100, 33105) Set wsh = Worksheets("sheet1") lastrow = wsh.UsedRange.Rows.Count For r = lastrow To 4 Step -1 If DeleteMe(wsh.Cells(r, 1).Value) Then _ wsh.Rows®.Delete Next r Application.ScreenUpdating = True End Sub Function DeleteMe(vValue) As Boolean Dim x As Integer DeleteMe = False For x = 1 To UBound(BadArray) If BadArray(x) = vValue Then DeleteMe = True Exit Function End If Next x End Function
-
WSalexanderd
AskWoody Lounger -
WSalexanderd
AskWoody LoungerNovember 23, 2003 at 3:53 pm #748004i am sorry i have taken so long to reply to you, but other projects have placed this one on the back burner.
if i use the code as a separate public sub all works ok, what i am having difficulties with now is being able to ” call DeleteBad ” from within my main programme. which unfortunately stops part way through the “Function DeleteMe” at “Ubound” have you any advise on this -
WSsdckapr
AskWoody LoungerNovember 23, 2003 at 11:09 pm #7481401) the function and the code need to be in the same module
2) the public (declaration) of “BadArray” must be in the declaration (before the first sub) of that same module.The public declaration allows the BadArray variable to be available to all routines in the module. It is filled in the program and used in the function.
Steve
-
WSsdckapr
AskWoody LoungerNovember 23, 2003 at 11:09 pm #7481411) the function and the code need to be in the same module
2) the public (declaration) of “BadArray” must be in the declaration (before the first sub) of that same module.The public declaration allows the BadArray variable to be available to all routines in the module. It is filled in the program and used in the function.
Steve
-
macropod
AskWoody_MVPNovember 25, 2003 at 5:31 am #748774Hi alexanderd,
Looking again at you problem, I noticed that the lines in your code:
wsh.Range(“P4”).FormulaR1C1 = _
“=IF(RC[-15]=33070,””yes””,IF(RC[-15]=33080,””yes””,IF(RC[-15]=33180,””yes””,IF(RC[-15]=33126,””yes””,IF(RC[-15]=33085,””yes””,IF(RC[-15]=33185,””yes””,IF(RC[-15]=33087,””yes””,””””)))))))”
all test the same cell (A4), as do the subsequent IF test lines. That being the case, you can get by with just one IF test, with no nesting involved, by incorporating an OR statement, thus:
wsh.Range(“P4”).Formula = _
“=IF(OR(A4=33070,A4=33080,A4=33085,A4=33087,A4=33090,A4=33091,A4=33093,A4=33094,A4=33095,A4=33097,A4=33099,A4=33100,A4=33101,A4=33105,A4=33126,A4=33135,A4=33136,A4=33150,A4=33180,A4=33185,A4=33190),””yes””,””””)”This does much the same as array-processing portion of Steve’s macro, except that it leaves the underlying formula accessible in the cells concerned.
Cheers
Cheers,
Paul Edstein
[Fmr MS MVP - Word] -
WSalexanderd
AskWoody LoungerNovember 26, 2003 at 8:03 pm #749656this is becoming serious an i am greatfull for all the input i agree that using
wsh.Range(“P4”).Formula = _
“=IF(OR(A4=33070,A4=33080,A4=33085,A4=33087,A4=33090,A4=33091,A4=33093,A4=33094,A4=33095,A4=33097,A4=33099,A4=ect ect dose work and at present i have reached a maximum of 29 items but to a slowing down of the end result.
i am now going to split this in half and see if it makes a difference.
Steve Aprahamian reply of 23 july is much quicker but as yet i have not found a way to call up the sub from in the main programe.
still trying to achieve a better product for all to use. -
WSsdckapr
AskWoody Lounger -
WSalexanderd
AskWoody Lounger -
WSsdckapr
AskWoody LoungerNovember 27, 2003 at 10:53 am #749859You didn’t really answer the question of HOW?
I will make a guess, if not what you are after, please post back:
With the worksheet viewable:
open the forms toolbar (View – toolbars – “check” Forms)
The item we are interested in is the “Command Button”
If Forms is docked this is the 4th item, if not docked it is the 2nd one down in the right column. [If you move your cursor over the items a “tool tip” will appear and it will say “Command Button”]
Click on the icon and then go to where on the worksheet you want to place it.
Press the left mouse button down when you are in the spot you want the upper left corner of the button to be, and while holding the left mouse button, drag the rectangular shape for the size of the button you want.
Release the left-mouse button and the “Assign macro” dialog will appear.
Select “DeleteBad” from the list
While the “button” is still selected Highlight the text “Button 1” and change the text to something more appropriate to tell the users what it is for. “Press Me to delete the bad rows” for example. If the text does not all fit, you can use the “handles” to change the size and shape.
You can close the forms toolbar if not docked (“X” in upper right) or goto view-tooolbars and uncheck it.Now when you press the button the macro will run. Be aware there is not undo for macros, so test it on a copy.
Steve -
WSalexanderd
AskWoody LoungerNovember 27, 2003 at 2:26 pm #749948what i am trying to do is replace the many lines of:-
wsh.range(“P4”).FormulaR1C1 = _
“=IF(RC[-15]=33070,””yes””,IF(RC[-15]=33080,””yes””,IF(RC[-15]=33180,””yes””,IF(RC[-15]=33126,””yes””,IF(RC[-15]=33085,””yes””,IF(RC[-15]=33185,””yes””,IF(RC[-15]=33087,””yes””,””””)))))))”
wsh.range(“P4”).copy Destination:=wsh.range(“P5:P6000”)
wsh.range(“Q4”).FormulaR1C1 = _
“=IF(RC[-16]=33090,””yes””,IF(RC[-16]=33190,””yes””,IF(RC[-16]=33091,””yes””,IF(RC[-16]=33093,””yes””,IF(RC[-16]=33095,””yes””,IF(RC[-16]=33094,””yes””,IF(RC[-16]=33101,””yes””,””””)))))))”
wsh.range(“Q4”).copy Destination:=wsh.range(“Q5:Q6000”)
wsh.range(“R4”).FormulaR1C1 = _
“=IF(RC[-17]=33099,””yes””,IF(RC[-17]=33097,””yes””,IF(RC[-17]=33150,””yes””,IF(RC[-17]=33135,””yes””,IF(RC[-17]=33136,””yes””,IF(RC[-17]=33100,””yes””,IF(RC[-17]=33105,””yes””,””””)))))))”with your module ” BadArray ” i know it works fine if i stop my program before the above and then run “BadArray” but cannot figure out how or where to introduce it.
-
WSsdckapr
AskWoody Lounger -
WSsdckapr
AskWoody Lounger -
WSalexanderd
AskWoody LoungerNovember 27, 2003 at 2:26 pm #749949what i am trying to do is replace the many lines of:-
wsh.range(“P4”).FormulaR1C1 = _
“=IF(RC[-15]=33070,””yes””,IF(RC[-15]=33080,””yes””,IF(RC[-15]=33180,””yes””,IF(RC[-15]=33126,””yes””,IF(RC[-15]=33085,””yes””,IF(RC[-15]=33185,””yes””,IF(RC[-15]=33087,””yes””,””””)))))))”
wsh.range(“P4”).copy Destination:=wsh.range(“P5:P6000”)
wsh.range(“Q4”).FormulaR1C1 = _
“=IF(RC[-16]=33090,””yes””,IF(RC[-16]=33190,””yes””,IF(RC[-16]=33091,””yes””,IF(RC[-16]=33093,””yes””,IF(RC[-16]=33095,””yes””,IF(RC[-16]=33094,””yes””,IF(RC[-16]=33101,””yes””,””””)))))))”
wsh.range(“Q4”).copy Destination:=wsh.range(“Q5:Q6000”)
wsh.range(“R4”).FormulaR1C1 = _
“=IF(RC[-17]=33099,””yes””,IF(RC[-17]=33097,””yes””,IF(RC[-17]=33150,””yes””,IF(RC[-17]=33135,””yes””,IF(RC[-17]=33136,””yes””,IF(RC[-17]=33100,””yes””,IF(RC[-17]=33105,””yes””,””””)))))))”with your module ” BadArray ” i know it works fine if i stop my program before the above and then run “BadArray” but cannot figure out how or where to introduce it.
-
WSsdckapr
AskWoody LoungerNovember 27, 2003 at 10:53 am #749860You didn’t really answer the question of HOW?
I will make a guess, if not what you are after, please post back:
With the worksheet viewable:
open the forms toolbar (View – toolbars – “check” Forms)
The item we are interested in is the “Command Button”
If Forms is docked this is the 4th item, if not docked it is the 2nd one down in the right column. [If you move your cursor over the items a “tool tip” will appear and it will say “Command Button”]
Click on the icon and then go to where on the worksheet you want to place it.
Press the left mouse button down when you are in the spot you want the upper left corner of the button to be, and while holding the left mouse button, drag the rectangular shape for the size of the button you want.
Release the left-mouse button and the “Assign macro” dialog will appear.
Select “DeleteBad” from the list
While the “button” is still selected Highlight the text “Button 1” and change the text to something more appropriate to tell the users what it is for. “Press Me to delete the bad rows” for example. If the text does not all fit, you can use the “handles” to change the size and shape.
You can close the forms toolbar if not docked (“X” in upper right) or goto view-tooolbars and uncheck it.Now when you press the button the macro will run. Be aware there is not undo for macros, so test it on a copy.
Steve -
WSalexanderd
AskWoody Lounger -
WSsdckapr
AskWoody Lounger -
WSalexanderd
AskWoody LoungerNovember 26, 2003 at 8:03 pm #749657this is becoming serious an i am greatfull for all the input i agree that using
wsh.Range(“P4”).Formula = _
“=IF(OR(A4=33070,A4=33080,A4=33085,A4=33087,A4=33090,A4=33091,A4=33093,A4=33094,A4=33095,A4=33097,A4=33099,A4=ect ect dose work and at present i have reached a maximum of 29 items but to a slowing down of the end result.
i am now going to split this in half and see if it makes a difference.
Steve Aprahamian reply of 23 july is much quicker but as yet i have not found a way to call up the sub from in the main programe.
still trying to achieve a better product for all to use. -
macropod
AskWoody_MVPNovember 25, 2003 at 5:31 am #748776Hi alexanderd,
Looking again at you problem, I noticed that the lines in your code:
wsh.Range(“P4”).FormulaR1C1 = _
“=IF(RC[-15]=33070,””yes””,IF(RC[-15]=33080,””yes””,IF(RC[-15]=33180,””yes””,IF(RC[-15]=33126,””yes””,IF(RC[-15]=33085,””yes””,IF(RC[-15]=33185,””yes””,IF(RC[-15]=33087,””yes””,””””)))))))”
all test the same cell (A4), as do the subsequent IF test lines. That being the case, you can get by with just one IF test, with no nesting involved, by incorporating an OR statement, thus:
wsh.Range(“P4”).Formula = _
“=IF(OR(A4=33070,A4=33080,A4=33085,A4=33087,A4=33090,A4=33091,A4=33093,A4=33094,A4=33095,A4=33097,A4=33099,A4=33100,A4=33101,A4=33105,A4=33126,A4=33135,A4=33136,A4=33150,A4=33180,A4=33185,A4=33190),””yes””,””””)”This does much the same as array-processing portion of Steve’s macro, except that it leaves the underlying formula accessible in the cells concerned.
Cheers
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
-
-
WSalexanderd
AskWoody LoungerNovember 23, 2003 at 3:53 pm #748005i am sorry i have taken so long to reply to you, but other projects have placed this one on the back burner.
if i use the code as a separate public sub all works ok, what i am having difficulties with now is being able to ” call DeleteBad ” from within my main programme. which unfortunately stops part way through the “Function DeleteMe” at “Ubound” have you any advise on this
-
-
-
WSalexanderd
AskWoody Lounger -
macropod
AskWoody_MVPJuly 31, 2003 at 1:53 am #698422Hi Alexander,
Do you really need to nest the IFs? For example:
=IF(A1=1,A1,””)&IF(A1=2,A1,””)&IF(A1=3,A1,””)&IF(A1=4,A1,””)&IF(A1=5,A1,””)&IF(A1=6,A1,””)&IF(A1=7,A1,””)&IF(A1=8,A1,””)&IF(A1=9,A1,””)&IF(A1=10,A1,””)&IF(A1=11,A1,””)&IF(A1=12,A1,””)
will evaluate 12 conditions without nesting, as will:
=IF(A1=1,A1,IF(A1=2,A1,IF(A1=3,A1,IF(A1=4,A1,IF(A1=5,A1,IF(A1=6,A1,””))))))&IF(A1=7,A1,IF(A1=8,A1,IF(A1=9,A1,IF(A1=10,A1,IF(A1=11,A1,IF(A1=12,A1,””))))))
with nesting. The result is text, which may not be appropriate, but that can be overcome by surrounding the formula with VALUE(). Then you only have the 1024 char limit to worry about.Cheers
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
-
-
-
notbrl
AskWoody LoungerJuly 30, 2003 at 9:36 pm #698333Chip Pearson addresses this very issue.
-
WSWassim
AskWoody LoungerJuly 30, 2003 at 10:57 pm #698379Alexanderd
To add to the great advice you have gotten already, in any case, when you find yourself using too many If statements, IMHO, its time to look at the CASE statement structure.
Look at it, and see how it works, you will like it.
But to answer your question think of teiring these Ifs, say:
If All first 7 Ifs turn True, then A1 will be True, then if the next 7 Ifs turn True, then B1 will be True, and if the next 7 Ifs turn True, then C1 will be True. Now based on A1, B1, and C1 is all are True then your next 7 Ifs can also be added, and you can go on and on.
You could produce a matrix of Trues and Falses, and work off of that, and there are no limits for that, other than the 65,536 cells in a worksheet, but I guess anything past 10 Ifs is confusing
enough, and again IMHO should not be used.
Hope this helps.
Wassim
-
DennisK50
AskWoody LoungerNovember 25, 2003 at 2:19 pm #748874Chip Pearson tackled this problem some time ago. You can find out how to get around the 7 if statement limitations by visiting his website and specifically this page…
http://www.cpearson.com/excel/nested.htm%5B/url%5D
Dennis
(Edited by HansV to make URL clickable – see Help 19)
-
DennisK50
AskWoody LoungerNovember 25, 2003 at 2:19 pm #748875Chip Pearson tackled this problem some time ago. You can find out how to get around the 7 if statement limitations by visiting his website and specifically this page…
http://www.cpearson.com/excel/nested.htm%5B/url%5D
Dennis
(Edited by HansV to make URL clickable – see Help 19)
Viewing 4 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Login screen icon
by
CWBillow
2 hours, 23 minutes ago -
AI coming to everything
by
Susan Bradley
4 hours ago -
Mozilla : Pocket shuts down July 8, 2025, Fakespot shuts down on July 1, 2025
by
Alex5723
6 hours, 10 minutes ago -
No Screen TurnOff???
by
CWBillow
6 hours, 32 minutes ago -
Identify a dynamic range to then be used in another formula
by
BigDaddy07
7 hours, 5 minutes ago -
InfoStealer Malware Data Breach Exposed 184 Million Logins and Passwords
by
Alex5723
18 hours, 42 minutes ago -
How well does your browser block trackers?
by
n0ads
4 hours, 59 minutes ago -
You can’t handle me
by
Susan Bradley
9 hours, 24 minutes ago -
Chrome Can Now Change Your Weak Passwords for You
by
Alex5723
4 hours, 28 minutes ago -
Microsoft: Over 394,000 Windows PCs infected by Lumma malware, affects Chrome..
by
Alex5723
1 day, 6 hours ago -
Signal vs Microsoft’s Recall ; By Default, Signal Doesn’t Recall
by
Alex5723
9 hours, 34 minutes ago -
Internet Archive : This is where all of The Internet is stored
by
Alex5723
1 day, 6 hours ago -
iPhone 7 Plus and the iPhone 8 on Vantage list
by
Alex5723
1 day, 6 hours ago -
Lumma malware takedown
by
EyesOnWindows
18 hours, 49 minutes ago -
“kill switches” found in Chinese made power inverters
by
Alex5723
1 day, 15 hours ago -
Windows 11 – InControl vs pausing Windows updates
by
Kathy Stevens
1 day, 15 hours ago -
Meet Gemini in Chrome
by
Alex5723
1 day, 19 hours ago -
DuckDuckGo’s Duck.ai added GPT-4o mini
by
Alex5723
1 day, 19 hours ago -
Trump signs Take It Down Act
by
Alex5723
2 days, 3 hours ago -
Do you have a maintenance window?
by
Susan Bradley
8 hours, 8 minutes ago -
Freshly discovered bug in OpenPGP.js undermines whole point of encrypted comms
by
Nibbled To Death By Ducks
1 day, 5 hours ago -
Cox Communications and Charter Communications to merge
by
not so anon
2 days, 6 hours ago -
Help with WD usb driver on Windows 11
by
Tex265
15 hours, 10 minutes ago -
hibernate activation
by
e_belmont
2 days, 15 hours ago -
Red Hat Enterprise Linux 10 with AI assistant
by
Alex5723
2 days, 19 hours ago -
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
2 days, 22 hours ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
2 days, 22 hours ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
1 day, 18 hours ago -
Out of band for Windows 10
by
Susan Bradley
3 days, 3 hours ago -
Giving UniGetUi a test run.
by
RetiredGeek
3 days, 10 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.