-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove Circular referance.bas
33 lines (30 loc) · 1.09 KB
/
Remove Circular referance.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Attribute VB_Name = "Module4"
Sub RemoveCircularReferences()
Dim ws As Worksheet
Dim cell As Range
Dim circularRefs As Range
' Loop through all sheets in the workbook
For Each ws In ThisWorkbook.Worksheets
' Loop through each cell in the sheet to check for circular references
For Each cell In ws.UsedRange
If Not IsError(cell.value) Then
On Error Resume Next
If cell.CircularReference.Address <> "" Then
If circularRefs Is Nothing Then
Set circularRefs = cell
Else
Set circularRefs = Union(circularRefs, cell)
End If
End If
On Error GoTo 0
End If
Next cell
Next ws
' Clear the contents of cells with circular references
If Not circularRefs Is Nothing Then
circularRefs.ClearContents
MsgBox "Circular references found and cleared."
Else
MsgBox "No circular references found."
End If
End Sub