循环浏览文件夹中的所有文件(遇到运行时错误' -2147221080(800401a8)')

时间:2017-04-07 08:17:02

标签: vba loops runtime-error

我正在运行一个代码,旨在从特定文件夹中的所有工作簿中提取数据。我已经编写了循环代码,与数据的提取代码分开。

Sub FixedIncomeLoop()

    Dim file            As Variant
    Dim wb1             As Workbook
    Dim wb2             As Workbook
    Dim folderpath      As String
    Dim ws              As Worksheet
    Dim ws2             As Worksheet

    Set wb1 = ActiveWorkbook
    Set ws = ActiveSheet

    folderpath = "C:\Users\Wei Hong\Documents\Trade Tickets\"

    file = Dir(folderpath)

    While (file <> "")
        Set wb2 = Workbooks.Open(file)
        Set ws2 = wb2.Worksheets("Trade Ticket")
        Call FixedIncome(wb1, ws, ws2)
        wb2.Close SaveChanges:=False
        Set wb2 = Nothing
    Wend

End Sub


Private Sub FixedIncome(wb1 As Workbook, ws As Worksheet, ws2 As Worksheet)

    Dim ws3 As Worksheet

    Set ws3 = wb1.Worksheets("Constants")  <-- facing error

    ws.Activate

...
End Sub

我正面临上面的运行时错误。不知道为什么!

0 个答案:

没有答案