I have several Excel files that I want to merge. Each file has 17 sheets, they are named 00, 01, 02, 03, etc.. Now, I open each of the workbook and try to copy the data into one file, that also has 16 sheets, on sheet x I want to have information from all of the other workbooks from sheet x.
Now, I open the file and for each file I have this code:
For i = 0 To 16
Workbooks(nazwaPliku).Activate
zakladka = Right("0" & CStr(i), 2)
Sheets(zakladka).Activate
ileWierszy = Application.WorksheetFunction.Max(Sheets(zakladka).Range("B:B"))
wierszMin = Application.WorksheetFunction.Match("1", Sheets(zakladka).Range("B:B"), 0)
zakresDoKop = "A" & wierszMin + 1 & ":" & "I" & wierszMin + ileWierszy + 1 '1 wiecej dla bezpieczenstwa
Sheets(zakladka).Range(zakresDoKop).Resize(ileWierszy, 1).Value = rok & "_" & czesc
Sheets(zakladka).Range(zakresDoKop).Copy
ThisWorkbook.Sheets(zakladka).Activate
ThisWorkbook.Sheets(zakladka).Range("A" & wsk(i)).PasteSpecial
wsk(i) = wsk(i) + ileWierszy + 2
Next i
For the first file everything is fine (wsk = 2 for all the sheets), but when I open the next workbook, something strange is happening. For example, when i=2, the code is copying data from sheets 02 - 16 and pasting them in thisworkboook. What's more, I've tested by deleting sheet 16 in my workbook and when i <> 16, an error occurred (as it was lacking one sheet). I think maybe I don't quite understand the loop here?
Any ideas? I know how i could write it differently but since i spent last 4 hours trying to figure things out, I would really like to know why is this happening.