Excel:检查工作簿内的工作表依赖性?

发布于 2024-10-18 12:13:28 字数 129 浏览 2 评论 0原文

我正在重构一个巨大的工作簿,其中有很多遗留部分、冗余计算、交叉依赖性等。

基本上,我试图删除不需要的工作表并在工作簿中实现一些适当的信息流。有没有一个好的方法来提取工作表之间的依赖关系(使用VBA)?

谢谢 马丁

I'm in the process of refactoring a huge workbook woth a lot of legacy parts, redundant computations, cross-dependencies etc.

Basically, I'm trying to remove unneeded sheets and implement some proper information flow within the workbook. Is there a good way to extract the dependencies between the sheets (with VBA)?

Thanks
Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-10-25 12:13:28

您可以使用 ShowPrecedents 和 NavigateArrow。
这是一些伪代码

for each oCell in oSht containing a formula
ocell.showprecedents
do until nomoreprecedents
i=i+1
Set oPrec = oCell.NavigateArrow(True, 1, i)
If not oPrec.Parent Is oSht Then
' off-sheet precedent
endif
loop
next ocell

You can use ShowPrecedents and NavigateArrow.
here is some pseudocode

for each oCell in oSht containing a formula
ocell.showprecedents
do until nomoreprecedents
i=i+1
Set oPrec = oCell.NavigateArrow(True, 1, i)
If not oPrec.Parent Is oSht Then
' off-sheet precedent
endif
loop
next ocell
夕嗳→ 2024-10-25 12:13:28

我想出了一个小潜艇来做到这一点。它将所有工作表移动到单独的工作簿中并打印出依赖关系。与使用 showPrecedents 相比,它的优点是它可以捕获所有链接,包括名称、嵌入的表单/图表等。

警告:移动工作表是不可撤消的,请在运行此操作之前保存工作簿并关闭(无需保存)并随后重新打开。

Sub printDependencies()
    ' Changes workbook structure - save before running this
    Dim wbs As VBA.Collection, wb As Workbook, ws As Worksheets
    Dim i As Integer, s As String, wc As Integer
    Set ws = ThisWorkbook.Worksheets
    Set wbs = New VBA.Collection
    wbs.Add ThisWorkbook, ThisWorkbook.FullName

    For i = ws.Count To 2 Step -1
        ws(i).Move
        wc = Application.Workbooks.Count
        wbs.Add Application.Workbooks(wc), Application.Workbooks(wc).FullName
    Next
    Dim wb As Workbook

    For Each wb In wbs
        For Each s In wb.LinkSources(xlExcelLinks)
            Debug.Print wb.Worksheets(1).Name & "<-" & wbs(s).Worksheets(1).Name
        Next
    Next
End Sub

该代码不是很优美或用户友好,但它可以工作。

I came up with a little sub to do this. It moves all the sheets into seperate workbooks and prints out the dependencies. The advantage over using showPrecedents is that it captures all links including names, embedded forms/diagramms etc.

Word of warning: Moving worksheets isn't undo-able, save your workbook before running this and close (without saving) and re-open afterwards.

Sub printDependencies()
    ' Changes workbook structure - save before running this
    Dim wbs As VBA.Collection, wb As Workbook, ws As Worksheets
    Dim i As Integer, s As String, wc As Integer
    Set ws = ThisWorkbook.Worksheets
    Set wbs = New VBA.Collection
    wbs.Add ThisWorkbook, ThisWorkbook.FullName

    For i = ws.Count To 2 Step -1
        ws(i).Move
        wc = Application.Workbooks.Count
        wbs.Add Application.Workbooks(wc), Application.Workbooks(wc).FullName
    Next
    Dim wb As Workbook

    For Each wb In wbs
        For Each s In wb.LinkSources(xlExcelLinks)
            Debug.Print wb.Worksheets(1).Name & "<-" & wbs(s).Worksheets(1).Name
        Next
    Next
End Sub

The code isn't very polished or user-friendly, but it works.

独留℉清风醉 2024-10-25 12:13:28

您可以按照以下链接的“查找单元格中使用的外部引用”主题中的步骤进行操作:

在工作簿中查找外部参考< /a>

但是,您应该输入要查找其依赖项的工作表的名称,而不是输入“[”。它将显示引用该工作表的每个单元格的大列表,但最终它可以工作。还没找到按Sheet分组的方法。

You can follow the steps at "Find external references that are used in cells" topic of the following link:

Find external references in a worbook

But instead of enter the "[" you should enter the name of the sheet you're trying to find its dependencies. It will display a large list of every single cell referencing the sheet, but at the end it works. Haven't find the way to group by Sheet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文