如何干净地断开 Excel VBA 2010 中的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15348503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:58:11  来源:igfitidea点击:

How to cleanly break links in Excel VBA 2010

excel-vbavbaexcel

提问by bob.mazzo

I'm trying to programmatically break all links, then recreate them in a workbook.

我正在尝试以编程方式断开所有链接,然后在工作簿中重新创建它们。

I got some VBA code to cleanly break Excel links in Excel 2010, however the trouble I always have is that Excel will prompt me every time I RECREATE the link.

我有一些 VBA 代码可以在 Excel 2010 中干净地断开 Excel 链接,但是我总是遇到的麻烦是每次我重新创建链接时 Excel 都会提示我。

Here's what I'm doing:

这是我在做什么:

Here's the BreakLinks procedure :

这是 BreakLinks 程序:

Sub BreakLinks()
    vLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)

    ' Break all links in the active workbook.
    For lLink = LBound(vLinks) To UBound(vLinks)
        ActiveWorkbook.BreakLink _
            Name:=vLinks(lLink), _
            Type:=xlLinkTypeExcelLinks
    Next lLink
end sub

Here's the Main section: *Please note that the last line where I assign thisFormula is where Excel prompts me with a File Dialog box which forces me to either cancel or choose my Master Workbook I'm linking back to...

这是主要部分: *请注意,我分配 thisFormula 的最后一行是 Excel 提示我使用文件对话框的地方,该对话框迫使我取消或选择我要链接回的主工作簿...

    BreakLinks    ' call proc to break the links
    thisFormula = "='[DataMaster - Data Template.xlsm]MASTER'!$B" + Trim(Str(myRow))     ' reference back to master workbook
    Range("A1").Formula = thisFormula       ' i.e. "='[DataMaster - Data Template.xlsm]MASTER'!$B32"
    Range("B1").Formula = "=D1"
    Range("C1").Formula = "=E1/100"
    thisFormula = "=BDH('[DataMaster - Data Template.xlsm]MASTER'!$A" + Trim(Str(myRow)) + "," + charCode + "PX_LAST" + charCode + ",FirstDate," + charCode + " " + charCode + "," + charCode + "cols=2;rows=50" + charCode + ")"
    Range("D1").Formula = thisFormula

I'd like to know how to avoid this very annoying Excel prompt, and just cleanly recreate my Formula/Link.

我想知道如何避免这个非常烦人的 Excel 提示,并干净地重新创建我的公式/链接。

回答by Ross McConeghy

You could try using Application.DisplayAlerts:

您可以尝试使用 Application.DisplayAlerts:

Application.DisplayAlerts = False
'<statements that may invoke alerts>
Application.DisplayAlerts = True