sdruley
Well-known Member
- Joined
- Oct 3, 2010
- Messages
- 557
- Office Version
- 365
- Platform
- Windows
I have been struggling for days trying to find the source of a circular reference. I tried the following code but it just sits there doing nothing. Can anyone find the glitch in this code or perhaps give me a better tool?
Code:
Sub FindCircRefs()
Set Source = ActiveSheet.Range("study")
' Get source information.
Sourcesheet = ActiveSheet.Name
Sheets.Add
' Get destination information.
destsheet = ActiveSheet.Name
destrange = ActiveCell.Address
' Return to source.
Worksheets(Sourcesheet).Activate
RowCount = 0
' Trap for error in "result", indicating no circular reference.
On Error GoTo notcircular
' Loop through every used cell in source.
For Each Item In Source
' Check to see if cell contains a formula.
If Left(Item.Formula, 1) = "=" Then
' If cell intersects with precedents, cell has circular
' reference.
result = Intersect(ActiveSheet.Range(Item.Address), _
ActiveSheet.Range(Item.Precedents.Address))
Worksheets(destsheet).Range(destrange).Offset(RowCount, _
0).Value = Item.Address(False, False)
Worksheets(destsheet).Range(destrange).Offset(RowCount, _
1).Value = "'" & Item.Formula
RowCount = RowCount + 1
' Skip to here if not circular.
skipitem:
End If
Next
Exit Sub
' If error in "result", go here.
notcircular:
' Skip cells that do not contain circular references.
Resume skipitem
End Sub