fixing code merge multiple file into one

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi,
i have about 3 files and the main file to merge data the code gives me error subscript out of range in this line Set ws = Workbooks("merge file").Worksheets("sheet1")
i'm sure about the name's main file
VBA Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim Path
Dim File
Dim wb As Workbook
Dim x
Dim lr, lr2
Dim ws As Worksheet
Set ws = Workbooks("merge file").Worksheets("sheet1")
Application.ScreenUpdating = False
With ws.Range("a2:r100000"): .ClearContents
.Activate
End With
Path = ThisWorkbook.Path & "\file\"
File = Dir(Path & "*.xlsx*")
    Do While File <> ""
    Set wb = Workbooks.Open(Path & File)
    If Not IsError(x) Then
             lr2 = wb.Worksheets(1).Cells(Rows.Count, "a").End(xlUp).Row
    wb.Worksheets(1).Range("a2:r" & lr2).Copy
    lr = ws.Range("a" & Rows.Count).End(3).Row + 1
    ws.Range("a" & lr).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
     End If
     wb.Close
          File = Dir
     Loop
     Application.ScreenUpdating = True
End Sub
 
every workbook it has sheet1 and contains data from a2:r10000 the same tidy so what you suggest
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Do the values for lr & lr2 show what you would expect as you step through the code?
 
Upvote 0
i follow by f8 i reaches in theses lines it shows me the value is empty for lr,lr2
VBA Code:
lr2 = wb.Worksheets(1).Cells(Rows.Count, "a").End(xlUp).Row
    wb.Worksheets(1).Range("a2:r" & lr2).Copy
    lr = ws.Range("a" & Rows.Count).End(3).Row + 1
    ws.Range("a" & lr).PasteSpecial xlPasteValues
 
Upvote 0
Are you saying that when you get to this line
VBA Code:
ws.Range("a" & lr).PasteSpecial xlPasteValues
the values for both lr & lr2 are shown as empty?
 
Upvote 0
Do you get any error messages?
Also is your code exactly the same as you posted?
 
Upvote 0
no error the macro runs normally but no data copys in merge file also this is the same code as in my post
 
Upvote 0
In that case there is something funny going on, because if both those variables are empty you should get an error on this line
VBA Code:
wb.Worksheets(1).Range("a2:r" & lr2).Copy
and on this line
VBA Code:
ws.Range("a" & lr).PasteSpecial xlPasteValues
 
Upvote 0
Fluff give me some times to attach my files and find out where is the problem if you have time
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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