如何使用vba自动取消文件打开提示

时间:2016-07-20 04:15:12

标签: excel vba excel-vba

我为excel中已关闭的工作簿的外部链接创建了动态文本字符串,需要转换为公式。有些文件尚不存在,但我需要创建为公式的链接。当我执行以下代码时,系统会提示您打开不存在的文件。有没有办法自动取消提示,以便输入公式无论如何?令人烦恼的是手动取消了大量的提示!

Sub GetData()
    Dim path As String, r As Single, c As Single
    Application.Calculation = xlCalculationManual
    For c = 0 To 29
        For r = 0 To 1000
            path = Worksheets("String formulas").Range("a3").Offset(r, c)
            Worksheets("Transformed data").Range("bl3").Offset(r, c).Formula = "=" & path
        Next r
    Next c
End Sub

2 个答案:

答案 0 :(得分:0)

尝试使用此更新的代码

Sub GetData()
    Dim path As String, r As Single, c As Single
    Set fso = CreateObject("scripting.filesystemobject")
    Application.Calculation = xlCalculationManual
    For c = 0 To 29
        For r = 0 To 1000
            path = Worksheets("String formulas").Range("a3").Offset(r, c)
            If fso.FileExists(path) = True Then
                Worksheets("Transformed data").Range("bl3").Offset(r, c).Formula = "=" & path
            End If
        Next r
    Next c
End Sub

答案 1 :(得分:0)

您可以通过切换Application.DisplayAlerts来解决这个问题。

Sub GetData()
    Dim path As String, r As Single, c As Single
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False

    For c = 0 To 29
        For r = 0 To 1000
            path = Worksheets("String formulas").Range("a3").Offset(r, c)
            Worksheets("Transformed data").Range("bl3").Offset(r, c).Formula = "=" & path
        Next r
    Next c

    Application.DisplayAlerts = True
End Sub