Find Circular References

jgarland

Board Regular
Joined
Nov 6, 2005
Messages
50
I found the following code online and slightly altered it for my use. I don't remember the source or I would give credit. The code loops through all sheets in the Workbook and finds circular references. This works perfectly but I need it to show a message if no circular references were found.

VBA Code:
Sub FindCircularReferences(control As IRibbonControl)
    Dim ws          As Worksheet
    Dim rng         As Range
    Dim msg         As String

    For Each ws In ThisWorkbook.Worksheets
        Set rng = ws.CircularReference
        If Not rng Is Nothing Then
            rng.Parent.Select
            rng.Select

            msg = "Circular reference found on sheet " & ws.Name & _
                  " in cell " & rng.Address(0, 0) & _
                  " with formula: " & vbLf & rng.Formula

            MsgBox msg, vbCritical, "Circular Reference Found"
            Exit For
        End If
    Next ws
MsgBox "Nothing Found", vbInformation, "Circular Reference" ' my added code
End Sub

I added a message box at the end of this code but it fires every time a reference is found. If I have multiple circular references then I need it to fire only after the last reference is found. Any and all help is greatly appreciated.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi there

I think you wold just need to move your message and add a Else Statement... test the below and let us know?

VBA Code:
Sub FindCircularReferences(control As IRibbonControl)
    Dim ws          As Worksheet
    Dim rng         As Range
    Dim msg         As String

    For Each ws In ThisWorkbook.Worksheets
        Set rng = ws.CircularReference
        If Not rng Is Nothing Then
            rng.Parent.Select
            rng.Select

            msg = "Circular reference found on sheet " & ws.Name & _
                  " in cell " & rng.Address(0, 0) & _
                  " with formula: " & vbLf & rng.Formula

            MsgBox msg, vbCritical, "Circular Reference Found"
            Exit For
            Else
            
         MsgBox "Nothing Found", vbInformation, "Circular Reference" ' my added code
        End If
    Next ws

End Sub
 
Upvote 0
Or
VBA Code:
(...)
Next ws

If Len(msg) > 0 Then
    MsgBox "Nothing Found", vbInformation, "Circular Reference"
End If

End Sub
Artik
 
Upvote 0
Thanks for the quick response.

Both Jimmypop & Artik revisions do not trigger the message if no circular references are found. Both revisions also will trigger the message after every circular reference is found.

What I am looking for is:
A. If no circular references are found, then show the "Nothing Found" message.
B. If a circular reference is found then I need the code to exit without triggering the message. This way I can eliminate the reference after each occurrence.

I just need the "Nothing Found" message to trigger one time, when no references are detected.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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