Hello all,
This is long, but my main question is in bold at the bottom. Thanks!
A couple of days ago I put a concept out there asking for help setting up a vba tool to search through cells to determine if they are correctly referencing other cells within their row. Many sheets in my workbook contain columns of copy-down formulas like a sumifs or index/match referencing a key in the same row as the formula. Here's a typical example:
in O12 the following formula: =IF(M12="yes",0,IF(OR(E12="06",E12="08",E12="10",E12="18",E12="32")=TRUE,G12/E12,0))
or the more complex
=IF(COUNTIF(Item_Lookup,D12)>0,EOMONTH(DATE(YEAR(B12)-ROUNDDOWN((SUMIF(Item_Lookup,D12,Prod_Month)+SUMIF(Item_Lookup,D12,Grow_Months))/12,0)-IF(MONTH(B12)<(INDEX('Item Overview'!$A$4:$O$997,MATCH(D12,Item_Lookup,0),MATCH("Calendar Month when saleable",'Item Overview'!$A$3:$O$3,0))-1),1,0),INDEX('Item Overview'!$P$4:$P$997,MATCH(D12,Item_Lookup,0),1),1),0),"Non-Production Liner")
You'll notice all the references within the sheet are to another cell in the same row.
Occasionally there is an error in sorting, copying, or pasting and we'll get one or all of the references looking in the wrong row. This doesn't throw an error, so unless we closely scrutinize the result, we get bad data.
My goal is to create a macro that loops through all the cells with formulas in a given range and flag those referring to cells in a different row.
Here is what I have so far:
This loops through all precedents in the range of rows and columns I have specified. As I understand the range.precedents property, it looks as deep as it can in references within the current sheet, which is great. Is there a way to make it evaluate only the immediate precedents? Those actually referred to in the formula, not the next level?
This is long, but my main question is in bold at the bottom. Thanks!
A couple of days ago I put a concept out there asking for help setting up a vba tool to search through cells to determine if they are correctly referencing other cells within their row. Many sheets in my workbook contain columns of copy-down formulas like a sumifs or index/match referencing a key in the same row as the formula. Here's a typical example:
in O12 the following formula: =IF(M12="yes",0,IF(OR(E12="06",E12="08",E12="10",E12="18",E12="32")=TRUE,G12/E12,0))
or the more complex
=IF(COUNTIF(Item_Lookup,D12)>0,EOMONTH(DATE(YEAR(B12)-ROUNDDOWN((SUMIF(Item_Lookup,D12,Prod_Month)+SUMIF(Item_Lookup,D12,Grow_Months))/12,0)-IF(MONTH(B12)<(INDEX('Item Overview'!$A$4:$O$997,MATCH(D12,Item_Lookup,0),MATCH("Calendar Month when saleable",'Item Overview'!$A$3:$O$3,0))-1),1,0),INDEX('Item Overview'!$P$4:$P$997,MATCH(D12,Item_Lookup,0),1),1),0),"Non-Production Liner")
You'll notice all the references within the sheet are to another cell in the same row.
Occasionally there is an error in sorting, copying, or pasting and we'll get one or all of the references looking in the wrong row. This doesn't throw an error, so unless we closely scrutinize the result, we get bad data.
My goal is to create a macro that loops through all the cells with formulas in a given range and flag those referring to cells in a different row.
Here is what I have so far:
VBA Code:
Sub MyPrec()
Dim wb As Workbook, ws As Worksheet
Dim myRange As Range
Dim rngPrecedents As Range, cell As Range
Dim rngPrecedent As Range
Dim row As Integer, lastrow As Integer, column As Integer, lastcolumn As Integer
Dim adrs As String
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Orders")
row = ws.Range("Order_Invoice_Num").row
lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).row
lastcolumn = ws.cells(row - 1, ws.Columns.Count).End(xlToLeft).column
For column = 1 To lastcolumn
For row = ws.Range("Order_Invoice_Num").row To lastrow
On Error GoTo Errorhandler
Set cell = ws.cells(row, column)
If Left(cell.Formula2, 1) = "=" Then
Set rngPrecedents = cell.Precedents
For Each rngPrecedent In rngPrecedents
If rngPrecedent.row <> cell.row Then
If adrs <> cell.Address Then
adrs = cell.Address
Debug.Print adrs
End If
End If
Next rngPrecedent
End If
Nextcell:
Next row
Next column
This loops through all precedents in the range of rows and columns I have specified. As I understand the range.precedents property, it looks as deep as it can in references within the current sheet, which is great. Is there a way to make it evaluate only the immediate precedents? Those actually referred to in the formula, not the next level?