【问题标题】:Prevent the "Update Values:" dialog box from opening every time a cell with a link is modified防止每次修改带有链接的单元格时打开“更新值:”对话框
【发布时间】:2022-03-12 01:43:09
【问题描述】:

快速版本:我正在处理的文件中有损坏的链接,因为它们指向其他人的硬盘。其他人的文件中的宏出错,该宏通过在公式前附加撇号将所有公式转换为文本。我写了一个宏来解决这个问题,但是文件中有大量的外部链接。该宏实质上将公式从下面的第一行更改为第二行,除了删除不必要的撇号外无济于事。

1) '='C:\OtherPersonsFolderPath\[FileName.xlsm]Sheet1'!A1
2)  ='C:\OtherPersonsFolderPath\[FileName.xlsm]Sheet1'!A1

如果我手动执行此操作,Excel 会打开一个对话框,要求我通过指向正确的文件来“更新 FileName.xlsm 中的值”。不过,我不想更新文件路径:我计划将其返回给文件的原始所有者,所有路径都保持完整,没有撇号。如果我点击该对话框上的“取消”按钮,我会得到预期的效果:公式会更新为我需要的内容,并且值会更改为它曾经是工作链接时的任何值。如果我每次弹出框时手动点击“取消”,它就可以正常工作,但是我有数千个单元格可以遍历数十张纸。我需要一种方法来告诉 VBA 在该框中说“取消”,或者首先阻止该框出现。有任何想法吗?我的代码如下:

Public Sub MyBugFix()

Application.Calculation = xlCalculationManual
'Note that I unsuccessfully tried options like "ThisWorkbook.UpdateLinks = xlUpdateLinksNever" and "Application.DisplayAlerts = False" here

Dim WS_Count As Integer
Dim I As Integer

WS_Count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_Count
    Sheets(I).Visible = True
    Sheets(I).Select
    Range("A1:BZ400").Select

    'Simple fix for embedded apostrophes in formulas (e.g., an equals sign in an IF statement)
        Selection.Replace What:="'=", Replacement:="=", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    'More complex fix for apostrophes at the start (they're special characters, so the find/replace trick doesn't work)
        Dim myRng As Range
        Dim myCell As Range
        Set myRng = Nothing
        On Error Resume Next
        Set myRng = Intersect(Selection, _
        Selection.Cells.SpecialCells(xlCellTypeConstants))
        On Error Resume Next

        For Each myCell In myRng.Cells
            If myCell.PrefixCharacter <> "" Then
            myCell.Value = "" & myCell.Text
            On Error Resume Next
            End If
        Next myCell
Next I

Application.Calculation = xlCalculationAutomatic

End Sub

【问题讨论】:

  • 是否在开头添加 Application.DisplayAlerts = False 并在最后添加 Application.DisplayAlerts = True ?
  • 如果将Application.EnableAlerts 设置为false 会发生什么?

标签: excel vba


【解决方案1】:

我在这里找到了一个解决方案:http://www.mrexcel.com/forum/excel-questions/506273-turn-off-update-values-dialog-box.html,所以我不能为此声明任何功劳!

在你编辑公式之前把它放在...

ThisWorkbook.UpdateLinks = xlUpdateLinksNever

然后在您进行编辑后重新打开它...

ThisWorkbook.UpdateLinks = xlUpdateLinksAlways

这为我解决了一个类似的问题,我使用 VBA 编写包含对其他电子表格的引用的单元格公式。所有功劳归于 MrExcel 论坛上的 AlphaFrog!

【讨论】:

    【解决方案2】:

    我发现了一个命令组合:

    ThisWorkbook.UpdateLinks = xlUpdateLinksNever 
    Application.DisplayAlerts = False
    

    '你的宏

    ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
    Application.DisplayAlerts = True
    

    最好的问候

    【讨论】:

    • 这和Loophole的回答一样吧?
    【解决方案3】:

    禁用选择工作表或更新值对话框:

                    

    这个简短的宏对Range.TextToColumns method 的使用应该会迅速清除整个工作簿中任何前缀刻度(又名'、单引号或Chr(39).PrefixCharacters。虽然该操作旨在针对已使用 ' 注释掉的公式,但它也会将看起来像数字的文本恢复为真实数字。

    Sub Clean_Prefix_Ticks()
        Dim w As Long, c As Long
    
        With ActiveWorkbook   '<- set this workbook properly if necessary
            For w = 1 To Worksheets.Count
                With Worksheets(w).UsedRange
                    For c = 1 To .Columns.Count
                        With .Columns(c)
                            If CBool(Application.CountA(.Cells)) Then _
                                .Cells.TextToColumns Destination:=.Cells(1), _
                                        DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
                        End With
                    Next c
                End With
            Next w
        End With
    End Sub
    

    删除Range.PrefixCharacter property 的大量性质允许文本到列操作扫过每个工作表的.UsedRange,而无需打开外部引用选择工作表对话框。

    如果有足够的需要,可以在操作发生之前轻松检查每一列。上面的代码是为处理工作簿范围的扫描而编写的。

    警告:TextFileColumnDataTypes property 设置为 xlGeneralFormat。请注意,您可能会丢失已分配的某些特殊单元格格式;尤其是字符串中字符的文本格式(例如Range .Characters property)。

    【讨论】:

      【解决方案4】:

      转到数据/编辑链接/启动提示,然后单击 不显示警报,也不更新自动链接。

      【讨论】:

        猜你喜欢
        • 2013-06-02
        • 2018-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多