每次修改带有链接的单元格时,都禁止打开"更新值:"对话框

use*_*188 6 excel vba excel-vba

快速版本:我在一个正在使用的文件中断了链接,因为它们指的是别人的硬盘.在其他人的文件中出现错误,该文件通过在公式之前附加撇号将所有公式转换为文本.我写了一个宏来解决这个问题,但文件中有大量的外部链接.宏基本上将公式从第一行更改为下面的第二行,只需删除不必要的撇号.

1) '='C:\OtherPersonsFolderPath\[FileName.xlsm]Sheet1'!A1
2)  ='C:\OtherPersonsFolderPath\[FileName.xlsm]Sheet1'!A1
Run Code Online (Sandbox Code Playgroud)

如果我手动执行此操作,Excel将打开一个对话框,要求我通过指向正确的文件在FileName.xlsm中"更新值".我不想更新文件路径:我打算将此文件路由回原始所有文件的所有者,sans撇号.如果我点击该对话框上的"取消"按钮,我会得到预期的效果:公式更新为我需要的内容,并且值会更改为当它是一个工作链接时所用的任何内容.如果我每次弹出时手动点击框中的"取消",它工作正常,但我已经有数千个单元格遍历数十张.我需要一种方法告诉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
Run Code Online (Sandbox Code Playgroud)

小智 0

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

\n\n

\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0选择图纸外部参考对话框

\n\n

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

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

删除Range.PrefixCharacter 属性的批量性质允许“文本到列”操作扫描每个工作表的.UsedRange,而无需引发外部引用“选择工作表”对话框。

\n\n

如果有足够的需求,可以在操作之前轻松检查每根柱子。编写上面的代码是为了处理工作簿范围的扫描。

\n\n
\n

注意:TextFileColumnDataTypes 属性设置为xlGeneralFormat。请注意,您可能会丢失指定的某些形式的特殊单元格格式;特别是字符串中字符的文本格式(例如Range .Characters 属性)。

\n
\n