Macro to count IF's in formulas?

billfinn

Board Regular
Joined
Jun 7, 2018
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
Good morning,
I've run into a bit of a snag with some cells that use IF nested as much as 24 levels deep. So far they haven't angered the Excel 2000 gods but that won't last. Does anyone have any advice for a macro to count the number of times IF occurs in each formula and report the cell location of the ones with more than 7 IF's in the formula? It's beyond my skills.
Very grateful for any assistance.

Thanks,
Bill
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
It's more than IFs, right? It's nesting of any combination of functions at 8 or more levels.
 
Upvote 0
SHG
That's correct. If I could just pull the formulas with more than 7 IFs along with their cell coordinates into a separate sheet I could review them to determine if they are nested or not. I know tthat all of the nested functions in this workbook are IF AND and IF OR. Nothing else is nested.
Any thoughts on how to do this?
Thanks much
Bill
 
Upvote 0
Actually I just need to know for one specific worksheet.
 
Upvote 0
Give this a whirl...
Code:
Sub List_IF()
Dim c As Range
Dim ws As Worksheet

Application.ScreenUpdating = False
Set ws = ActiveSheet
Sheets.Add(After:=ActiveSheet).Name = "IF"

For Each c In ws.UsedRange.SpecialCells(xlCellTypeFormulas)
    If (Len(c.Formula) - Len(Replace(c.Formula, "IF(", ""))) / 3 > 7 Then Sheets("IF").Cells(Rows.Count, 1).End(xlUp).Offset(1) = c.Address
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks much njimack!
That gives me the cell address which is a great start, it would be very helpful to have the actual formula pasted in the cell next to the cell address.
I tried adding this line at the end of the If statement
Code:
:[COLOR=#333333]Sheets("IF").Cells(Rows.Count, 1).End(xlUp).Offset(2) = c.formula[/COLOR]
but it didn't work.
I'm not sure how to get the actual formula to paste somewhere as text vs the value
Thanks,
Bill
 
Upvote 0
Try
Code:
Then Sheets("IF").Cells(Rows.Count, 1).End(xlUp).Offset(1).resize(,2).value = array(c.Address,Replace(c.Formula, "=", ""))
 
Upvote 0
Fluff,
Thanks a million! That gave me exactly what I need.
Thanks,
Bill
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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