0

我有一个包含 10 个 excel 文件 + 1 个带有宏的文件的文件夹。我只打开宏。如何获取位于宏文件(使用vba)的文件夹中所有其他文件的列表,以及如何一一打开和关闭它们?

4

1 回答 1

1

This should get you on the right track. These macros will loop through folder and open or close all files in the folder.

    Sub LoopThroughFilesAndOpen()
        Dim strFile As String
        Dim strPath As String
        Dim colFiles As New Collection
        Dim i As Integer

        strPath = "put your directory here"
        strFile = Dir(strPath)

        While strFile <> ""
            colFiles.Add strFile
            strFile = Dir
        Wend

        'List filenames in Column A of the active sheet
        If colFiles.Count > 0 Then
            For i = 1 To colFiles.Count
                ActiveSheet.Cells(i, 1).Value = colFiles(i)
                Workbooks.Open Filename:=strPath & colFiles(i)
            Next i
        End If

    End Sub

    Sub LoopThroughFilesAndClose()
        Dim strFile As String
        Dim strPath As String
        Dim colFiles As New Collection
        Dim i As Integer

        strPath = "put your directory here"
        strFile = Dir(strPath)

        While strFile <> ""
            colFiles.Add strFile
            strFile = Dir
        Wend

        'List filenames in Column A of the active sheet
        If colFiles.Count > 0 Then
            For i = 1 To colFiles.Count
                ActiveSheet.Cells(i, 1).Value = colFiles(i)
                'Workbooks.Close Filename:=strPath & colFiles(i)
                Workbooks(colFiles(i)).Close SaveChanges:=False
            Next i
        End If

    End Sub

Good Luck. - just put in the directy you want e.g. - "C:\myfolder\"

EDIT/ADDITION:

If you want the macros to automatically use the directy of the active workbook(the one that your running the macro from) then you can just use these versions of the above macros:

Sub LoopThroughFilesAndOpen()
    Dim strFile As String
    Dim strPath As String
    Dim colFiles As New Collection
    Dim i As Integer

    strPath = ActiveWorkbook.Path & "\"
    strFile = Dir(strPath)

    While strFile <> ""
        colFiles.Add strFile
        strFile = Dir
    Wend

    'List filenames in Column A of the active sheet
    If colFiles.Count > 0 Then
        For i = 1 To colFiles.Count
            ActiveSheet.Cells(i, 1).Value = colFiles(i)
            Workbooks.Open Filename:=strPath & colFiles(i)
        Next i
    End If

End Sub

Sub LoopThroughFilesAndClose()
    Dim strFile As String
    Dim strPath As String
    Dim colFiles As New Collection
    Dim i As Integer

    strPath = ActiveWorkbook.Path & "\"
    strFile = Dir(strPath)

    While strFile <> ""
        colFiles.Add strFile
        strFile = Dir
    Wend

    'List filenames in Column A of the active sheet
    If colFiles.Count > 0 Then
        For i = 1 To colFiles.Count
            ActiveSheet.Cells(i, 1).Value = colFiles(i)
            'Workbooks.Close Filename:=strPath & colFiles(i)
            Workbooks(colFiles(i)).Close SaveChanges:=False
        Next i
    End If

End Sub

Which changes this line:

    strPath = "put your directory here"

To:

    strPath = ActiveWorkbook.Path & "\"

Note:

These two macros were taken and modified from: http://www.vadriano.com/excel-vb/2007/04/21/how-to-loop-through-files-in-a-folder/

于 2012-09-24T14:31:30.193 回答