Hey Guys

I'm still new to VBA and I've run into some trouble with a loop I've been writing.

What I basically need it to do is this:

I have a master file from which I run the VBA. Then I have 20 worksheets, which all corresponds in name to 20 workbooks (Excel-files) that are placed in the same folder as the master file.

What I need the VBA code to do, is to open each of the workbooks, copy/paste two distinct areas / ranges from that open workbook to the master file (the copy and paste area is the same), and then close all those other workbooks again.

Fairly simple.

But I simply can't get it to work.

Here is the code so far:

Sub Import_Data()


' Variables used to store woorksheet names and paths.

Dim myPath As String, Master As String

' Path is set for use when we need to locate the input files.
' Master workbook filename is saved for when we need to activate it during paste process.

myPath = ThisWorkbook.Path
Master = ActiveWorkbook.Name

' The array "filename" contains refernces to locate the correct excel-files from which the input data is copied.
' Each array set contains the name of the input file which also corresponds to the worksheet in the master file
' where we will insert the input data

Dim sheetname(1 To 20) As String

sheetname(1) = "P1"
sheetname(2) = "P2"
sheetname(3) = "P3"
sheetname(4) = "4"
sheetname(5) = "5"
sheetname(6) = "6"
sheetname(7) = "7"
sheetname(8) = "8"
sheetname(9) = "9"
sheetname(10) = "10"
sheetname(11) = "11"
sheetname(12) = "12"
sheetname(13) = "13"
sheetname(14) = "14"
sheetname(15) = "15"
sheetname(16) = "16"
sheetname(17) = "17"
sheetname(18) = "18"
sheetname(19) = "19"
sheetname(20) = "20"


Dim filename(1 To 20) As String

filename(1) = "\P1.xls"
filename(2) = "\P2.xls"
filename(3) = "\P3.xls"
filename(4) = "\4.xls"
filename(5) = "\5.xls"
filename(6) = "\6.xls"
filename(7) = "\7.xls"
filename(8) = "\8.xls"
filename(9) = "\9.xls"
filename(10) = "\10.xls"
filename(11) = "\11.xls"
filename(12) = "\12.xls"
filename(13) = "\13.xls"
filename(14) = "\14.xls"
filename(15) = "\15.xls"
filename(16) = "\16.xls"
filename(17) = "\17.xls"
filename(18) = "\18.xls"
filename(19) = "\19.xls"
filename(20) = "\20.xls"


For ifilename = LBound(filename) To UBound(filename)

For isheetname = LBound(sheetname) To UBound(sheetname)

'Copy & Paste B12: E36

Workbooks.Open (myPath & ifilename)
Sheets(isheetname).Select
    Range("B12: E36").Select
    Selection.Copy
Windows(Master).Activate
Sheets(isheetname).Select
    Range("B12").Select
    ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
        IconFileName:=False

'Copy & Paste H12: K36

Windows(isheetname).Activate
Sheets(isheetname).Select
    Range("H12: K36").Select
    Selection.Copy
Windows(Master).Activate
Sheets(isheetname).Select
    Range("H12").Select
    ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
        IconFileName:=False
Windows(isheetname).Activate
ActiveWorkbook.Close

Next isheetname
Next ifilename


End Sub
It seems that the issue is generated when trying to open the different files. It's like it doesn't recognize the "\" and instead tries to open an xls file with the name of the folder the files are in + the 1, then 2, then 3, etc.

If any of you can figure out what the problem is. I would greatly appreciate it!

Thanks in advance.