In my VBA code I have a Do While
loop which is supposed to copy all (all but first row) data from different excel files with the extension "*.xlsx". All these files contain the same format only with different values (they are export files from my companies PDM software). This needs to be done to create a spare parts list.
When I press run on my script and it's finished it almost always leaves row 2 to 25 empty which is not supposed to happen, its only supposed to leave row 2 empty (also empty rows in between copied data). I need those empty rows to designate which list is which machine and in turn to which machine a spare part belongs.This is what happens and it's wrong. Not only that but it also does not copy everything.
But when I run my loop step by step it DOES work and only leaves an empty row on row 2 and copies all.
Here's what a to-be-imported-list looks like.
I have tried searching for fixes on stack overflow and the internet for hours but could no find anything on this subject.
Do While Len(Filename) > 0
'Error -> Handler
On Error GoTo Handler
Set wbk = Workbooks.Open(Path & Filename)
' finding last row so program knows the range it needs to copy.
LastRowSource = Workbooks(Filename).Sheets("Exported PRO.FILE Data").Cells(Rows.Count, 1).End(xlUp).Row
If LastRowSource = 1048576 Or LastRowSource = 0 Then
LastRowSource = 1
End If
'copy entire document
wbk.Sheets("Exported PRO.FILE Data").Range("A2:Z" & LastRowSource + 1).Copy
ThisWorkbook.Activate
' finding last row so program knows the range it needs to copy underneath.
LastRowMaster = Cells(Rows.Count, 1).End(xlUp).Row
If LastRowMaster = 1048576 Or LastRowMaster = 0 Then
LastRowMaster = 1
End If
'Paste values
Sheets("Gathersheet").Range("A" & LastRowMaster + 2).PasteSpecial xlPasteValues
wbk.Close True
Filename = Dir
Loop
I expect the output to be a complete list, which is supposed to look like this. I really hope you can help me stack overflow geniusses.
Thank you in advance, Wouter