文件循环错误地跳过文件

时间:2016-04-29 15:38:54

标签: vba excel-vba loops filepath excel

我编写了以下代码来遍历目录中的所有文件,并从中复制某些值并将其粘贴回主文件中。

我遇到的问题是代码永远不会完全遍历所有文件,我从来没有收到过错误。

如图所示,文件名称为1 - #####,然后是2 - ####等。

有时第一个数字中有多个像图片中有两个1 - ###'但结束数字仍然不同。

问题在于,代码不是按照实际的数字顺序,而是仅使用第一个数字,从1,10,11,100开始,并完全跳过其余的数字。

关于如何解决这个问题的任何想法?

Sub ReadDataFromCloseFile()
  On Error GoTo ErrHandler

  Application.EnableEvents = False
  Application.ScreenUpdating = False

  Dim FileType As String
  Dim FilePath As String

  FileType = "*.xlsm*" 'The file type to search for
  FilePath = "\\filepath\" 'The folder to search

  Dim src As Workbook
  Dim OutputCol As Variant
  Dim Curr_File As Variant

  OutputCol = 9 'The first row of the active sheet to start writing to

  Curr_File = Dir(FilePath & FileType)

  Do Until Curr_File = ""
    ' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
    Set src = Workbooks.Open(FilePath & Curr_File, True, True)

    Sheets("Reporting").Range("I7:I750").Copy

    Workbooks("Master.xlsm").Activate
    Sheets("Sheet2").Select
    Sheets("Sheet2").Cells(4, OutputCol).Select
    ActiveCell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False
    OutputCol = OutputCol + 1

    ' CLOSE THE SOURCE FILE.
    src.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Curr_File = Dir
  Loop
  Set src = Nothing

  Application.EnableEvents = True
  Application.ScreenUpdating = True

  ErrHandler:
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub

Filepath

3 个答案:

答案 0 :(得分:0)

我不知道它为什么不打开2到9个文件。此版本将所有文件路径放入集合中,然后逐步完成集合。 它也不会在粘贴之前选择纸张等。

Sub ReadDataFromCloseFile()

    Dim FileType As String
    Dim FilePath As String
    Dim colFiles As Collection
    Dim src As Workbook
    Dim tgt As Workbook
    Dim OutputCol As Variant
    Dim Curr_File As Variant

    Set colFiles = New Collection

    FileType = "*.xlsm*" 'The file type to search for
    FilePath = "\\filepath\" 'The folder to search

    EnumerateFiles FilePath, FileType, colFiles

    OutputCol = 9 'The first row of the active sheet to start writing to

    'If Master.xlsm is the book containing this code then use '=ThisWorkbook'
    Set tgt = Workbooks("Master.xlsm")

    For Each Curr_File In colFiles
        Set src = Workbooks.Open(Curr_File, True, True)

        src.Worksheets("Reporting").Range("I7:I750").Copy
        tgt.Worksheets("Sheet2").Cells(4, OutputCol).PasteSpecial xlPasteValuesAndNumberFormats
        OutputCol = OutputCol + 1

        src.Close False

    Next Curr_File

End Sub

Sub EnumerateFiles(ByVal sDirectory As String, _
    ByVal sFileSpec As String, _
    ByRef cCollection As Collection)

    Dim sTemp As String

    sTemp = Dir$(sDirectory & sFileSpec)
    Do While Len(sTemp) > 0
        cCollection.Add sDirectory & sTemp
        sTemp = Dir$
    Loop
End Sub

答案 1 :(得分:0)

使用下面的循环循环访问文件可能更容易。

Sub LoopFiles()

Dim FSO As New FileSystemObject
Dim Fldr As Folder
Dim Fl As File

'Loop through files in folder
For Each Fl In FSO.GetFolder(filePath).Files
    'Check for file type
    If Fl.Type = "Excel Macro-Enabled Workbook" Then
        'Open file & do procedure
    End If
Next
Set FSO = Nothing

End Sub

答案 2 :(得分:0)

尝试将变量OutputCol声明为整数而不是变量。如果您知道您的数据总是一个数字,那么使用变体绝不是一个好主意。执行代码需要更多资源,并且您不知道幕后发生的所有内部逻辑。它还可以让您更好地控制代码的执行,并且可能不会让您感到头疼。如果您不知道要输出的数据,请仅使用变体。

希望这有帮助!