Opening and Looping through files via VBA

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
72,265
Office Version
  1. 365
Platform
  1. Windows
I would like to create a macro that does the following that will combine an undetermined amount of data files into one file. I envision it work something like this:

1. Allows the user to browse a directory and select/open as many files in the directory as they wish (using CTRL to select multiple files). They could select any number of files.

2. Loop through all the files, copy and paste their data to one file, and close all the files (other than new "combined file"). The trick here is that I only want to loop through the files that were just opened. If the user had any Excel files open on their computer prior to running this macro, I want to ignore those files.


I know how to browse and open ONE file, and I know how to copy and paste the data via VBA, I just need to find out how to open multiple files, and loop through those files just opened. Can anyone help?

Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You will have to play with this a little, but a starting point is to count the workbooks using:

Test = Workbooks.Count

Once you know how many are currently open, you can then use something like:

Test = Workbooks.Count
Chdir .... (indicate which directory to get the files from)
Workbooks.open (ie open up the "Open" box for them to choose files)

Do until workbooks.count = Test
Workbooks(Test).select
Insert macro here to copy and paste

loop
end sub
 
Upvote 0
tactps,

Thanks for the reply. Though I don't think the solution would help due to the following conditions I laid out in the original question:

The trick here is that I only want to loop through the files that were just opened. If the user had any Excel files open on their computer prior to running this macro, I want to ignore those files.

I imagine that this macro that I am writing will be contained in a separate Excel file, and I want to account for the fact that the user may have other Excel files open on their computer that I wish to ignore. I imagine that I can use an IF statement to exclude the macro Excel file, but am not sure how to account for any other open Excel files.

Basically, I want the macro to run ONLY on the files that they select in the beginning of the macro, not every single open Excel file.
 
Upvote 0
jmiskey said:
I know how to copy and paste the data via VBA, I just need to find out how to open multiple files, and loop through those files just opened.
One way:

Sub Test1()
Dim var As Variant, i As Integer
var = Application.GetOpenFilename(FileFilter:="Excel files (*.xls), *.xls", MultiSelect:=True)
On Error GoTo ERRORHANDLER
For i = 1 To UBound(var)
Workbooks.Open (var(i))

MsgBox "Your copy and paste code goes here."

ActiveWorkbook.Close False
Next i
Exit Sub
ERRORHANDLER:
MsgBox "No files were selected, action cancelled."
End Sub
 
Upvote 0
Hi j,

How about:
Code:
Sub SelectOpenCopy()
    Dim vaFiles As Variant
    Dim i As Long
    Dim wbkToCopy As Workbook
    
    vaFiles = Application.GetOpenFilename("Excel Files (*.xls), *.xls", _
              Title:="Select files", MultiSelect:=True)
    
    If IsArray(vaFiles) Then
        For i = LBound(vaFiles) To UBound(vaFiles)
            Set wbkToCopy = Workbooks.Open(FileName:=vaFiles(i))
            '
            'copy, paste, whatever
            '
            wbkToCopy.Close savechanges:=False
        Next i
    End If

End Sub
HTH

EDIT : Tom was TDQ! :biggrin:
 
Upvote 0
Tom & Rich,

Thanks. That was what I was looking for! :biggrin:
 
Upvote 0
I have a slightly similar issue. I need to loop through two separate directories, open the first files from each, do some copy and paste work with them and then close both files. Then this process needs to loop so the second files in each folder open and so on. This is where I'm having my issue. I'm calling the Dir function twice for each separate folder, but apparently this doesn't work. Basically right now, I have one of the folders looping correctly but the other one is either stagnant on the same file or returning a run time error. Please help!
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top