尝试从宏中打开多个工作表时,关闭弹出窗口

时间:2019-03-27 21:40:09

标签: excel vba

我试图从多个工作表中提取一些数据,但是当我运行宏时,一些工作表出现了一个弹出窗口,要求我更新或不更新工作表中附加的链接。我正在寻找一种使宏命中“不更新”的方法

P.S。如果可能的话,我希望sFolder成为根文件夹,并搜索C:\ temp

中包含的所有“ * .xlsx”
Sub tgr()

    Dim wbDest As Workbook
    Dim wsDest As Worksheet
    Dim rCopy As Range
    Dim sFolder As String
    Dim sFile As String
    Dim lRow As Long

    Set wbDest = ThisWorkbook                   'The workbook where information will be copied into
    Set wsDest = wbDest.Worksheets("Sheet1")    'The worksheet where information will be copied into
    sFolder = "C:\Path\" 'The folder path containing the xlsx files to copy from

    'would like sFolder to be the root folder and also 
    '   search for any "*.xlsx" contained inside C:\temp

    lRow = 1 'The starting row where information will be copied into

    'Adjust the folder path to ensure it ends with \
    If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"

    'Get the first .xlsx file in the folder path
    sFile = Dir(sFolder & "*.xlsx")

    'Begin loop through each file in the folder
    Do While Len(sFile) > 0

        'Open the current workbook in the folder
        With Workbooks.Open(sFolder & sFile)
            'Copy over the formulas from A1:C3 from only the first 
            '   worksheet into the destination worksheet
            Set rCopy = .Sheets(1).Range("C9:D26")
            wsDest.Cells(lRow, "A").Resize(rCopy.Rows.Count, rCopy.Columns.Count).Formula = rCopy.Formula

            'Advance the destination row by the number of rows being copied over
            lRow = lRow + rCopy.Rows.Count

            .Close False    'Close the workbook that was opened from the folder without saving changes
        End With
        sFile = Dir 'Advance to the next file
    Loop

End Sub

1 个答案:

答案 0 :(得分:1)

更改

With Workbooks.Open(sFolder & sFile)

收件人

With Workbooks.Open(sFolder & sFile, UpdateLinks:=False)

有关此主题的更多信息: How to suppress Update Links warning?