0

This is my macro for merging all the files in one directory to one excel sheet. I am unable to find why its not working as per expectation. I have thousand rows in each file. It copies 1000 rows from first file but in next file it copies from 1001th row instead of 1st row. in same way 3rd file copies from 2001th row. I want to copy first 1000 rows every while. Which part of code causing problem

Sub MergeFiles()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
Dim i
i = 0

'change folder path of excel files here
Dim TTFiles_Path As String
ThisWorkbook.Worksheets(7).Activate
TTFiles_Path = Range("B2").Value

Set dirObj = mergeObj.GetFolder(TTFiles_Path)
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)


'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Range("A4:IV" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(8).Activate

'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next

End Sub
3
  • I can't see where you use the variable i. How many rows are in the first file? Why should it be limited to only 1000 rows?
    – Dave
    Commented Aug 9, 2014 at 8:56
  • Each and every file have 1000 rows. I have only rows of data so its limited to 1000 rows. I haven't yet used i, i'll use it in pasting data with offset
    – Pavi
    Commented Aug 9, 2014 at 10:44
  • I'd expect Range("A65536").End(xlUp).Offset(1, 0) (two places) to accomodate for basic offset/non-overlap. You might not need i.
    – Hannu
    Commented Aug 9, 2014 at 10:51

1 Answer 1

0

One way to analyze the fault: Use debugging on the macro.

Please see my answer here for the very basics.

1
  • The linked posting is something I created in the progress of including this "answer". NOTE: I felt that it was at a more general level than a direct answer here, and therefore added it as a standalone Q/A (I didn't find something similar in searching).
    – Hannu
    Commented Aug 11, 2014 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .