每次修改包含链接的单元格时,请不要打开“更新值:”对话框

快速版本:我正在处理的文件中存在断开的链接,因为它们指向别人的硬盘。 一个macros在其他人的文件错误,通过在公式之前附加撇号将所有公式转换为文本。 我写了一个macros来解决这个问题,但是文件中有很多的外部链接。 macros从本质上改变了从第一行到第二行的公式,除了去除不必要的撇号之外别无他法。

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

如果我手动执行此操作,Excel会打开一个对话框,要求我通过指向正确的文件在FileName.xlsm中“更新值”。 我不想更新文件path:我打算把这个返回给文件的原始所有者,所有的path都是机智的,没有撇号。 如果我点击那个对话框上的“取消”button,我会得到预期的效果:公式更新到我所需要的值,并且这个值改变为当它是一个工作链接时返回的值。 它可以正常工作,如果每次popup时都手动点击“取消”,但是我有数以千计的单元格遍历数十张表单。 我需要一种方法来告诉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 (eg, 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 

我在这里find了一个解决scheme: http : //www.mrexcel.com/forum/excel-questions/506273-turn-off-update-values-dialog-box.html,所以我不能要求它的任何功劳!

把这个放在编辑公式之前

ThisWorkbook.UpdateLinks = xlUpdateLinksNever

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

ThisWorkbook.UpdateLinks = xlUpdateLinksAlways

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

禁用“select图纸或更新值”对话框:

选择图纸外部参考对话框

这个短的macros使用Range.TextToColumns方法应该快速地从整个工作簿中清除任何前缀打勾(aka,单引号或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属性的批量特性允许Text-to-Columns操作扫描每个工作表的.UsedRange,而不会引发外部参照“ select工作表”对话框。

如果有足够的需求,在操作发生之前,每列可以很容易地被检查。 上面的代码是为了处理工作簿范围的扫描而编写的。

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