简体   繁体   中英

How to automatically cancel file open prompts using vba

I have dynamic text strings created for external links to closed workbooks in excel that need to be converted to formulas. Some of the files do not yet exist, but I need the links created as formulas. When I execute the following code, I get prompted to open the non-existent file. Is there a way to automatically cancel the prompts so that the formula is entered regardless? It's annoying cancelling a gazillion prompts manually!

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

Try with this updated code

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

You can get around this by toggling 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM