【发布时间】: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 会发生什么?