0

I have a folder with 10 excel files + 1 file with macro. I open only macro. How can I get the list of all other files in folder where situated macro file (with vba) and how can I open and close them one by one?

2
  • you should examine the examples of excel-help to the topics of filesystemobject. this is a bigger task, you might want to look into this code too: cpearson.com/excel/GetFileName.aspx Commented Sep 24, 2012 at 13:51
  • Read up on DIR function. Commented Sep 24, 2012 at 14:17

1 Answer 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/

Sign up to request clarification or add additional context in comments.

3 Comments

They will also put the names of each file in the active workbook/worksheet cells - you can just delete that code if you don't want that. It happens from this code: ActiveSheet.Cells(i, 1).Value = colFiles(i) -Good Luck
Nice answer. I forgot that Dir loops this way. One thing that you indicated, but is worth emphasizing, is that strPath must end with a "\", or better yet, Application.PathSeparator.
Yea, I was googling "how to get the string directory of the active workbook".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.