Accurately identify cells in the selected range that are referenced by formulas in other sheets

I am new to VBA and I have been struggling to build a macro that accurately identifies cells in the selected range that are referenced by formulas in other sheets and selects only those cells that fulfill that criteria.

the macro I have been able to build with help returns mixe results, sometimes returning cells that dont have any direct dependents in other sheets, and sometimes not selecting those.

please help me fixing the code and understand what went wrong.

Sub FindDependentsInOtherSheets()
    Dim ws As Worksheet
    Dim cell As Range
    Dim rng As Range
    Dim wsSource As Worksheet
    Dim formulaCell As Range
    Dim outputRange As Range
    Dim dependentFound As Boolean
    Dim formulaText As String
    Dim formulaCells As Range
    Dim cellAddress As String

    Set rng = Selection
    If rng Is Nothing Then
        MsgBox "Please select a range first."
        Exit Sub
    End If

    Set wsSource = rng.Worksheet

    ' Initialize outputRange to Nothing
    Set outputRange = Nothing

    ' Loop through each cell in the selected range
    For Each cell In rng
        If IsEmpty(cell) Then
            GoTo NextCell
        End If
        
        dependentFound = False
        cellAddress = "'" & wsSource.Name & "'!" & cell.Address(False, False)
        
        ' Loop through each worksheet in the active workbook to find formulas referencing the cell
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> wsSource.Name Then
                On Error Resume Next
                Set formulaCells = ws.UsedRange.SpecialCells(xlCellTypeFormulas)
                On Error GoTo 0
                If Not formulaCells Is Nothing Then
                    For Each formulaCell In formulaCells
                        formulaText = formulaCell.Formula
                        ' Check if the formula references the cell
                        If InStr(1, formulaText, cellAddress, vbTextCompare) > 0 Then
                            dependentFound = True
                            Exit For
                        End If
                    Next formulaCell
                End If
                If dependentFound Then Exit For
            End If
        Next ws
        
        ' If a dependent is found in another sheet, add the cell to the output range
        If dependentFound Then
            If outputRange Is Nothing Then
                Set outputRange = cell
            Else
                Set outputRange = Union(outputRange, cell)
            End If
        End If

NextCell:
    Next cell

    ' Select the output range if any cells found
    If Not outputRange Is Nothing Then
        outputRange.Select
        MsgBox "Cells that are direct precedents to other sheets have been selected.", vbInformation
    Else
        MsgBox "No direct precedents found in other sheets for the selected range.", vbInformation
    End If

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

I exepected to loop through each cell in the selected range and check if there are any formulas in other sheets that reference the cells in my selected range. If a dependent is found, we’ll add the cell to the output range. Finally, if any cells are found, we’ll select the output range.

the end result thus, is the selection of cells from my selected range that had dependents

New contributor

Aprendiz de Programador is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật