Open Multiple Files in Same Dir

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
I have found this great code someone had wrote that when ran, allows you to select certain files in the same directory and run the same macro/procedure on each of them - in my case is just a straight copy and paste. The problem I am having with it I believe is related to the targeted 'paste sheet' or master sheet.

-on the master sheet when all of the data is cleared out and I run the below, the 1st source file runs fine, but after that it overwrites itself on the 2nd source file and > instead of a (xlDown) type of procedure.
- if I have anything <>""" in the first row of data input (in my case its cell "B2" because I have headers), then the below works exactly what I am expecting.

I am not sure why this is; it could be because I have the master sheet in a Table format?

--hopefully I have explained this clear enough



Code:
Sub SelectOpenCopy()
    Dim vaFiles As Variant
    Dim i As Long
    Dim wb As Workbook
    Dim File_Path As String
    File_Path = Range("File_Path").Value
    
    vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", _
              Title:="Select files", MultiSelect:=True)
              
    
    If IsArray(vaFiles) Then
        For i = LBound(vaFiles) To UBound(vaFiles)
            Set wb = Workbooks.Open(filename:=vaFiles(i))
            ''BSQUAD code
            wb.Worksheets("FI Medical").Select
                Range("A2:AB2").Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.Copy
            Windows("" & File_Path & "").Activate
                Sheet33.Select
                    Range("B1").End(xlDown).Offset(1).Select
                    Selection.PasteSpecial Paste:=xlPasteColumnWidths
                    Selection.PasteSpecial Paste:=xlPasteValues
            wb.Application.CutCopyMode = False
            wb.Close savechanges:=False
            ''BSQUAD code
        Next i
    End If
Sheet33.Range("B1").Select
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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